Tutorial:How to - Install Python packages in a virtual environment (virtualenv)
0
2
Entering edit mode
6.8 years ago
vimalkvn ▴ 320

To setup virtual Python environments (virtualenv's) and install packages in them, the following programs are required:

The following package is not a requirement. It is used in the steps below and is recommended:

Please follow the steps below to get these packages installed on Linux (Ubuntu 17.10) or on Windows 7.

Install required packages - Linux (Ubuntu 17.10)

A package manager (Synaptic, apt) can be used to install the following

  • Python 3 should be installed by default. If not, install the python3 package.
  • Install python3-virtualenv and virtualenvwrapper.

An example installation using apt:

sudo apt install python3 python3-virtualenv virtualenvwrapper

Install required packages - Windows 7

  • Install .NET Framework 3.6 or higher using Windows update or visit this page on Microsoft's website. This package is a requirement for Build tools for visual studio 2017 which should be installed next.
  • Install Microsoft Build Tools for Visual Studio 2017 which includes compilers and libraries required for building packages installed using pip. More information is available in this wiki page on the Python website.
  • Python 3 - Download latest release from the Python website. As of this writing, the latest release is Python 3.6.3. Download the 32-bit (x86) or 64-bit (x86-64) executable installer depending on your system. Once the installer is launched, choose the option to add python to the PATH as in the screenshot below:
  • Install virtualenv and virtualenvwrapper-win from a command prompt:

     pip install virtualenv virtualenvwrapper-win
    

To demonstrate the installation of a local Python package, I will use cutadapt as an example. cutadapt is used to remove adpater sequences from sequencing reads.

Install cutadapt in a virtualenv - Linux (Ubuntu 17.10)

Note: cutadapt is available in the Ubuntu and Debian package repositories as python3-cutadapt. It is used here for the purpose of demonstration. Other Python packages can be installed in the same manner.

Create a virtual environment:

vimal@ubuntu:[~]: mkvirtualenv -p /usr/bin/python3 py3env

Python 2 is the default version so we need to specify the path to Python 3 binary using the -p option. Once the virtual environment py3env is created, it will be activated automatically. Note the presence of the environment name (py3env) in the prompt:

 (py3env) vimal@ubuntu:[~]:

Install cutadapt using pip:

(py3env) vimal@ubuntu:[~]: pip install cutadapt

The cutadapt command should now be available:

 (py3env) vimal@ubuntu:[~]: cutadapt --version
 1.15

To exit from the virtual environment, type deactivate:

 (py3env) vimal@ubuntu:[~]: deactivate
 vimal@ubuntu:[~]:

To use cutadapt again, the virtual environment py3env will need to be activated. The workon command can be used for this purpose:

vimal@ubuntu:[~]: workon py3env
(py3env) vimal@ubuntu:[~]:

Install cutadapt in a virtualenv - Windows 7

Create a virtual environment:

C:\Users\vimal> mkvirtualenv py3env

Once the virtual environment py3env is created, it will be activated automatically. Note the presence of the name (py3env) in the prompt:

(py3env) C:\Users\vimal>

Install cutadapt using pip:

(py3env) C:\Users\vimal> pip install cutadapt

The cutadapt command should now be available:

(py3env) C:\Users\vimal> cutadapt --version
1.15

To exit from the virtual environment, type deactivate:

(py3env) C:\Users\vimal> deactivate
C:\Users\vimal>

To use cutadapt again, the virtual environment py3env will need to be activated. The workon command can be used for this purpose:

C:\Users\vimal> workon py3env
(py3env) C:\Users\vimal>

Additional programs (Python 3) can be installed in the same virtualenv or a new virtualenv can be created and used if necessary.

ubuntu cutadapt virtualenv python windows • 7.5k views
ADD COMMENT

Login before adding your answer.

Traffic: 1342 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6