Working with multiple project or in a an already configured system, using virtual environment will protect you against messing up the initial configuration. Don’t use pip with sudo unless you setup for the first time your system wide virtual environment package/configuration. Otherwise, it may cause a discrepancy among existing versions.

To get started with pip let’s install it:

  • Debian:
    — For python 2 —
    apt install python-pip
    — For python 3 —
    apt install python3-pip
  • Centos:
    yum install epel-release
    yum install python-pip

Using some basic pip commands you can list packages, show a package details, install or uninstall packages.

  • pip list, shows installed packages.
  • pip show package_name, shows detail of a given package and its location.
  • pip uninstall package_name, uninstall a package.

To use pip’s specific version i.e with python3.x, use pip3 instead of pip. Because pip is checking your default path which is defined in your system confiugration to locate your python executable command.

python3 -m pip install requests, is able to locate your python3 directory tree and install packages within this directory otherwise, pip alone, will take into consideration your default python path, generally the path used for python2.7, as this version comes with default linux installations mostly.

Check the example below (Remember using sudo is not a good idea but sometimes I do :))  )

We have requests package version 2.6.0 installed for python 2.7 (It’s default python version coming with system installation).

And for python version 3 we installed requests package version “2.24.0”.

Otherwise, you encounter version or dependency conflicts and get an error message. So, that’s why it’s a best pratice to work with virtual environment.

Virtual Environment Setup & Usage

As you see I’m using python2.7 here. I installed a virtual environment for python 3 too. “python3 -m pip install virtualenv”. Now check the difference:

I’ve two virtual environment for both python versions.

Now it’s time create our first customized virtual environment with a dedicated directory.

I’ll setup another virtual environment, this time for python 3.

virtualenv -p python3 examples_py3

-p sets python interpreter.

examples_py3 is the name of directory. If it doesn’t exist, will be created.

Before starting to work with virtual environment you must firstly activate it. Under examples_py3 directory we have bin/activate.sh. For windows under examples_py3 we have Scripts/activate.bat file. Run the script (don’t execute source it), prompt will be marked with virtual environment name. Type “deactivate” to return back in your terminal.

Our python interpreter has version 3.6.8

I’ve a script requiring “request” package. I’ll install this package on my virtual environment and run the script.

Of course installing one or two packages is not a big deal but what if you have more than 3 packages to install ? You can handle multiple package installation by one command with the help of requirements.txt file.

python -m pip freeze command prints all packages already installed. you can redirect the output to requirements.txt file and share this with your collegues that you work together on the same project. Later you can install all packages with python -m pip install -r requirements.txt.

Inside this requirements file I’ll add a new package (just append in last line), flask framework then we will see how to install them from requirement.txt file.

Flask==1.1.2

Virtualenvwrapper

It searches and detects virtual environments installed on the computer. Install virtuealenvwrapper with sudo python -m pip install virtualenvwrapper then append the virtualenvwrapper.sh in your user’s .profile or .bash_profile file.

After restart your shell session, “virtualenvwrapper.sh” script will be sourced. It gives you to use “workon” command, your existing virtual environments under “/home/your_user/virt_envs_python” will be printed.

  • workon virtual_environment_name, activates the environment.

If you want to setup a dedicated workspace for your projects you can append folloqing line to your ~/.profile or ~/.bash_profile file:

PROJECT_HOME=”/home/your_user/virt_envs_python”

Then create folder “/home/your_user/virt_envs_python”

  • mkproject new_project, creates new_project directory under your workspace and setup a dedicated virtual environment for this project. By this way, even if you are inside another file system, when activating your virtual environment you will be redirected to your workspace. But it doesn’t work for manually configured virtual environments. To do so, you should type “setvirtualenvproject” command when you activated your virtual environment and you are inside the directory which belongs to your environment.

Ex:

Pipenv

The other option to work with virtual environments is installing “pipenv”. This creates under your working directory a virtual environment, install packages and dependencies. This can be useful when you work with others on a project together. Like “requirements.txt”, pipenv keeps informations about your interpreter, packages,versions and many other specifications.

  • sudo pip install pipenv, to install.
  • Go to your working directory. Ex: cd /home/projects/web_project
  • pipenv install pacakge_1 package_2 package_n, you can install one or more packages at one time.
  • In your working directory you will find two files: Pipfile (all configuration of your environment) and Pipfile.lock (contains hashes and package details).
  • When exiting from your virtual environment, type “exit” instead of “deactivate”.
  • pipenv shell, to work with virtual environment.
  • pipenv install –three, installs everything for python 3. Check also your pip file to configure your interpreter if it’s required.

It has more user friendly interface. If you work with github you should put Pipfile and Pipfile.lock in your repository. Because when a developper or another person clones your project, this person can install all requirements from Pipfile by type simple command “pipenv install”. All packages and dependencies related to this project will be installed.

When you install a new package, your Pipfile and Pipfile.lock are automatically updated.

  • pipenv update –outdated, to update all packages. In my case, accorfing to my system and Pipfile everything is up to date.

  • pipenv install requests~=1.1, installs given version of a package.
  • pipenv install “requests>=1.2”, installs the package equivalent to 1.2 or greater version.