Difference between revisions of "Install Python Packages in a Python virtual environment"

From CAC Documentation wiki
Jump to navigation Jump to search
Line 7: Line 7:
Users can '''create''' as many virtual environments, each in their own directory, as needed.
Users can '''create''' as many virtual environments, each in their own directory, as needed.
* python2: <code>python -m virtualenv <your virtual environment directory></code>
* python2: <code>python2 -m virtualenv <your virtual environment directory></code>
* python3: <code>python3 -m venv <your virtual environment directory></code>
* python3: <code>python3 -m venv <your virtual environment directory></code>

Revision as of 15:38, 16 April 2021

Manage Modules in Your Python Virtual Environment

Users can manage their own python environment (including installing needed modules) using virtual environments (works with python2 and python3). Please see the documentation on virtual environments on python.org for details.

Create Virtual Environment

Users can create as many virtual environments, each in their own directory, as needed.

  • python2: python2 -m virtualenv <your virtual environment directory>
  • python3: python3 -m venv <your virtual environment directory>

Activate Virtual Environment

Next you need to activate a virtual environment before using it:

source <your virtual environment directory>/bin/activate

Install Python Modules Using pip

After activating your virtual environment, you can now install python modules for the activated environment:

  • NOTE: It's always a good idea to update pip first:
pip install --upgrade pip
  • Install the module:
pip install <module name>
  • List installed python modules in the environment:
pip list modules

Sample workflow

  • Example: Create a new environment, and install tensorflow and keras:
$ module load python3   
        (note: python3 is an example, the actual module on the cluster you are using may be different)
        (use the "module avail" command to view the list of python versions you can load; it might be something like python/3.6)
$ python3 -m venv tensorflow
$ source tensorflow/bin/activate
(tensorflow)$ pip install --upgrade pip
Collecting pip
  Using cached https://files.pythonhosted.org/packages/30/db/9e38760b32e3e7f40cce46dd5fb107b8c73840df38f0046d8e6514e675a1/pip-19.2.3-py2.py3-none-any.whl
Installing collected packages: pip
  Found existing installation: pip 18.1
    Uninstalling pip-18.1:
      Successfully uninstalled pip-18.1
Successfully installed pip-19.2.3

(tensorflow)$ pip install tensorflow keras
Collecting tensorflow
  Using cached https://files.pythonhosted.org/packages/de/f0/96fb2e0412ae9692dbf400e5b04432885f677ad6241c088ccc5fe7724d69/tensorflow-1.14.0-cp36-cp36m-manylinux1_x86_64.whl
Successfully installed absl-py-0.8.0 astor-0.8.0 gast-0.2.2 google-pasta-0.1.7 grpcio-1.23.0 h5py-2.9.0 keras-2.2.5 keras-applications-1.0.8 keras-preprocessing-1.1.0 markdown-3.1.1 numpy-1.17.1 protobuf-3.9.1 pyyaml-5.1.2 scipy-1.3.1 six-1.12.0 tensorboard-1.14.0 tensorflow-1.14.0 tensorflow-estimator-1.14.0 termcolor-1.1.0 werkzeug-0.15.5 wheel-0.33.6 wrapt-1.11.2

(tensorflow)$ pip list modules
Package              Version
-------------------- -------
absl-py              0.8.0  
astor                0.8.0  
gast                 0.2.2  
google-pasta         0.1.7  
grpcio               1.23.0 
h5py                 2.9.0  
Keras                2.2.5  
Keras-Applications   1.0.8  
Keras-Preprocessing  1.1.0  
Markdown             3.1.1  
numpy                1.17.1 
pip                  19.2.3 
protobuf             3.9.1  
PyYAML               5.1.2  
scipy                1.3.1  
setuptools           40.6.2 
six                  1.12.0 
tensorboard          1.14.0 
tensorflow           1.14.0 
tensorflow-estimator 1.14.0 
termcolor            1.1.0  
Werkzeug             0.15.5 
wheel                0.33.6 
wrapt                1.11.2