Setting up Python development environment

Setting up Python development environment

Install Python

It’s easy to install Python in your system. Nany Linux and UNIX distributions nowaday already include Python. The first thing you may do is checking if Python was already installed in your system.

On terminal (or cmd if you are Windows user), type below command to check:

    $ python -V
    Python 3.6.5 :: Anaconda, Inc.

If you get some thing like bash: python: command not found when execute above command, you haven’e had Python installed. You need to download and install it. Go to Python download page and chose the most stable version (the versioon that has highest number and isn’t marked as an alpha or beta release) and download it. Since the last version of Python 2 only receive necessary security updates until 2020, it is highly recommended to use Python 3.

Create Python virtual environment

There are some ways to create Python virtual environment

Using virtualenv

virtualenv is a tool to create isolated Python environments. It creates a directory that contains all the necessary executables and doesn’t share those executables with other virtualenv environments.

The first thing you have to do is install virtualenv. If you are on Windows and you installed Python throught install bundle, you already had virtualenv installed on your system. On other hand, you can manually install it using pip

    $ pip install virtualenv

Verify that virtualenv is successfully installed

    $ virtualenv --version
    16.0.0

Suppose your project is located at ~/my-project

  1. Create virtual environment for your project:

    $ cd ~/my-project
    $ virtualenv my-project-env
    

    virtualenv my-project-env creates my-project-env folder that contains executables, pip. In this case, the name of the virtual environment is my-project-env. If you would like to put Python library files in current folder(my-project folder), you can do it by ignoring the env name parameter

    $ virtualenv
    or
    $ virtualenv .
    

    To chose a specific Python interpreter:

    $ virtualenv -p ~/python3.6/bin/python3.6 my-project-env
    
  2. Using virtual environment

    You have to activate the virtual environment in order to use it

    On Unix system, you can do it using:

    $ source my_project/my-project-env/bin/activate
    

    On Windows:

    Scripts\activate
    
  3. Deactivate virtual environment

    To deactivate a virtual environment, just use deactivate

    $ deactivate
    
  4. Delete virtual environment

    To delete a virtual environment, just delete the virtual environment folder, in this case, it is my-project-env

    $ rm -rf my-project-env
    

    Or if you are using Windows, just delete my-project-env folder as you did with any normal folder.

Last modified October 4, 2020