How to setup a Python working environment - Ubuntu

By JoeVu, at: 22:35 Ngày 02 tháng 5 năm 2023

Thời gian đọc ước tính: 13 min read

How to setup a Python working environment - Mac
How to setup a Python working environment - Mac

1. Introduction

Python is a powerful programming language that is widely used for developing applications in a variety of domains, including web development, data analysis, and artificial intelligence. If you are a Python developer working on Ubuntu, it is important to set up a local development environment that is tailored to your needs. By doing so, you can take advantage of the many benefits that a local development environment offers.

Firstly, setting up a local development environment allows you to isolate your Python project from the system's Python installation. This means that you can have different versions of Python and Python packages installed for different projects without worrying about conflicts. Additionally, a local development environment provides you with greater control over your project dependencies and allows you to easily manage and update them.

Secondly, a local development environment provides you with a familiar and comfortable workspace that is tailored to your needs. You can choose your favorite code editor, customize your development environment to your liking, and streamline your workflow with the tools and utilities that you prefer.

Finally, setting up a local development environment allows you to work offline and avoid potential network issues that may arise when working on a remote server. This means that you can continue working on your project even when you don't have an internet connection.

In this article, we will guide you through the process of setting up a Python working environment in Ubuntu. We will cover the installation of Python, setting up a virtual environment, choosing an editor, setting up iPython, and using a debugging tool. By the end of this article, you will have a fully functional Python working environment that is tailored to your needs.
 

2. Install Python

Python is likely already installed on your Ubuntu system, but it is a good idea to make sure that you have the latest version installed. In addition, if you want to work with both Python 2 and Python 3, you may want to install both versions on your system. Here are the steps to install Python 2 and Python 3 on Ubuntu:

Install Python 2

  • Open the terminal and run the following command: sudo apt-get install python2.7
  • Verify that Python 2 is installed by running the command: python2.7 --version

Install Python 3

  • Open the terminal and run the following command: sudo apt-get install python3
  • Verify that Python 3 is installed by running the command: python3 --version

Now that you have both Python 2 and Python 3 installed on your system, you can manage them easily using the update-alternatives command. This command allows you to choose which version of Python is the default on your system. Here are the steps to manage your Python installations:

List the available Python installations by running the command: sudo update-alternatives --list python (This will output a list of installed Python versions, including their paths)

Choose the default Python version by running the command: sudo update-alternatives --config python (This will display a list of available Python versions and prompt you to select the default version)

Verify the default Python version by running the command: python --version (This will output the version of Python that is currently set as the default on your system)

By following these steps, you can easily manage your Python installations and ensure that you are using the correct version for your projects.

We will see how to manage different python versions easily in the next parts
 

3. Setup Virtual Environment

Setting up a virtual environment for your Python project is essential to avoid conflicts between different projects and their dependencies. A virtual environment allows you to install and manage project-specific dependencies separately from the global Python installation on your system. Here are the steps to set up a virtual environment for your project
 

3.1 Virtualenv

To install virtualenv, open the terminal and run the following command

sudo apt-get install virtualenv


Create a virtual environment

Navigate to your project directory and run the following command: virtualenv venv

This will create a new virtual environment in a folder named venv.

Activate the virtual environment

source venv/bin/activate


Now, you can install project-specific dependencies using pip inside the virtual environment without affecting other projects or the global Python installation on your system. However, if you need to switch between different versions of Python, you can use tools like pyenv and pyenv-wrapper to make the process easier.


3.2 Pyenv

pyenv is a tool that allows you to easily install and switch between different versions of Python on your system. Here are the steps to install and use pyenv

Install pyenv 

curl https://pyenv.run | bash


Install Python versions using pyenv

pyenv install --list  # list all python versions
pyenv install [version]  # change version to python version that you want to install, ex: 3.11.0


Switch between Python versions using pyenv

pyenv versions  # show all versions
pyenv global [version]  # to set the default global Python version on your system.
pyenv local [version]  # to set the Python version for a specific project.

 

4. Choose Editor

Choosing the right code editor for your Python project is crucial for productivity and efficient development. Here are some popular code editors for Python and their pros and cons
 

4.1 Visual Studio Code (VS Code)

  • Download link: https://code.visualstudio.com/download
  • Pros: Lightweight, customizable, great debugging support, large community, and extensions marketplace.
  • Cons: Some features may require configuration or extensions, may consume a lot of resources.
     

4.2 PyCharm

  • Download link: https://www.jetbrains.com/pycharm/download/
  • Pros: Powerful, easy-to-use, great debugging support, code completion, and refactoring tools.
  • Cons: Heavier and slower than other editors, requires a paid license for some features.
     

4.3 Sublime Text

  • Download link: https://www.sublimetext.com/download
  • Pros: Lightweight, fast, customizable, and has a large number of plugins available.
  • Cons: Limited debugging support, not as feature-rich as other editors.
     

4.4 Atom

  • Download link: https://atom.io/
  • Pros: Open source, customizable, and has a large community of developers.
  • Cons: Limited debugging support, may require configuration or extensions for advanced features.

Choose an editor that best suits your needs and preferences, and make sure to explore its features and customization options to maximize your productivity.

In addition to the editors mentioned above, there are also other popular editors such as Emacs and Vim that have excellent support for Python development. Ultimately, the choice of editor depends on your personal preferences and the requirements of your project.

In the next section, we will discuss how to set up and use iPython for interactive Python development.
 

5. iPython Setup

iPython is an interactive shell for Python that provides a more user-friendly and efficient way to interact with Python code than the default Python interpreter. Here are some pros and cons of using iPython:

Pros:

  • Interactive shell with enhanced features such as tab completion, history, and object introspection.
  • Easy to test code snippets and explore APIs.
  • Ability to run shell commands directly from the iPython shell.

Cons:

  • Requires additional setup compared to the default Python interpreter.
  • May not be necessary for all Python developers.

To set up iPython, first, you need to install it using pip, the package installer for Python

pip install ipython


Once installed, you can launch iPython from the terminal by typing ipython.

To test iPython, you can try the following code snippet:

x = 5
y = 7
x + y


Output will be 

12


This will calculate the sum of x and y and print the result in the output prompt.

In addition to the standard iPython shell, there are also other tools available that extend the functionality of iPython, such as Jupyter Notebook and JupyterLab. These tools allow you to create interactive notebooks that mix code, documentation, and visualizations, making it a great tool for data analysis and data science.

To install Jupyter Notebook, you can use pip

pip install jupyter


Once installed, you can launch Jupyter Notebook from the terminal by typing jupyter notebook.

To install JupyterLab, you can also use pip

pip install jupyterlab


Once installed, you can launch JupyterLab from the terminal by typing jupyter lab.

Overall, iPython and its related tools are powerful and versatile tools for Python development, and can greatly enhance your productivity and efficiency when working with Python code.

In the next section, we will discuss how to use a debugging tool to help you identify and fix errors in your Python code.
 

6. Use Debugging Tool

Debugging is an essential skill for any software developer, and Python provides several debugging tools that can help you identify and fix errors in your code. One of the most popular debugging tools for Python is pdb (Python Debugger).

pdb allows you to step through your code line by line, set breakpoints, and inspect variables to help you understand how your code is executing and identify any errors that may be occurring.

To use pdb, you first need to import it into your Python code:

import pdb


You can then set a breakpoint in your code using the pdb.set_trace() function:

def my_function(x, y):
    z = x + y
    pdb.set_trace()
    return z

result = my_function(2, 3)
print(result)


When the code reaches the pdb.set_trace() line, it will pause execution and drop you into the pdb debugger, where you can inspect variables, step through code, and interact with the Python interpreter.

In Python 3.7 and later, a new breakpoint() function was introduced that provides a simpler way to set a breakpoint in your code. Instead of using pdb.set_trace(), you can simply call the breakpoint() function at any point in your code where you want to pause execution

Here are some basic pdb commands you can use:

  • n: Execute the current line and move to the next line.
  • s: Step into a function call.
  • r: Continue execution until the end of the current function.
  • c: Continue execution until the next breakpoint is encountered.
  • p: Print the value of a variable.
  • q: Quit the debugger and exit the program.

pdb is a powerful tool, but it can take some time to get used to. However, with practice, you will be able to use it to quickly identify and fix errors in your Python code.

In addition to pdb, there are also other debugging tools available for Python, such as ipdb (iPython Debugger) and pycharm (an integrated development environment with built-in debugging tools).
 

7. Conclusion

In conclusion, setting up a Python working environment in Ubuntu can greatly enhance your Python development experience. By installing Python, setting up a virtual environment, choosing an editor, setting up iPython, and using a debugging tool, you will have everything you need to start writing Python code and debugging any issues that may arise.


Theo dõi

Theo dõi bản tin của chúng tôi và không bao giờ bỏ lỡ những tin tức mới nhất.