EWS Labs, Python Virtual Environments for EWS
Follow these steps to create a custom python environment for your course. For more information about our Linux module system please visit https://answers.uillinois.edu/81201.
Creating a virtual python environment
These two modules contain the basic install along with pip and setuptools.
[netid@linux-a2 ~]$ module load python3/3.4.1
[netid@linux-a2 ~]$ module load python/2.7.10
From here you can go about creating your own custom python install into your home directory.
[netid@linux-a2 ~]$ virtualenv <env-name> # this will create and setup the virtual environment into the directory specified
[netid@linux-a2 ~]$ virtualenv /home/netid/my-env-1
To activate and begin working in your virtual python environment.
[netid@linux-a2 ~]$ source <env-name>/bin/activate
[netid@linux-a2 ~]$ source /home/netid/my-env-1/bin/activate
Once you have activated your environment you can use the standard pip to manage packages within that environment.
(my-env-1) [netid@linux-a2 ~]$ pip install <package-name>
(my-env-1) [netid@linux-a2 ~]$ pip install nltk Collecting nltk Downloading nltk-3.1.tar.gz (1.1MB) 100% |████████████████████████████████| 1.1MB 89kB/s Building wheels for collected packages: nltk Running setup.py bdist_wheel for nltk Stored in directory: /home/netid/.cache/pip/wheels/dc/8a/6c/764cd4ca5530 Successfully built nltk Installing collected packages: nltk Successfully installed nltk-3.1
You can see in the above examples that the name of your current environment is printed before your bash prompt. This makes it easier to track where you are when working with multiple virtual environments.
To leave your virtual environment and return to your normal environment:
(my-env-1) [netid@linux-a2 ~]$ deactivate [netid@linux-a2 ~]$
You can create multiple different environments as needed. Additionally, you can export your environment so that another person may load the exact same environment to test your code.
While your environment is activated this is done via:
(my-env-1) [netid@linux-a2 ~]$ pip freeze > /path/to/text/file.txt
(my-env-1) [netid@linux-a2 ~]$ pip freeze > /home/netid/my-env-1-freeze.txt nltk==3.1 wheel==0.24.0 (my-env-1)[netid@linux-a2 ~]$
When another person wishes to load said environment the command would be as follows:
[netid@linux-a2 ~]$ virtualenv <env-name> [netid@linux-a2 ~]$ source <env-name> /bin/activate (env-name)[netid@linux-a2 ~]$ pip install –r /path/to/text/file.txt
[netid@linux-a2 ~]$ virtualenv /home/netid/import-env New python executable in /home/netid/import-env/bin/python Installing setuptools, pip, wheel...done. [netid@linux-a2 ~]$ source import-env/bin/activate (import-env)[netid@linux-a2 ~]$ pip install -r /home/netid/my-env-1-freeze.txt Collecting nltk==3.1 (from -r /home/netid/my-env-1-freeze.txt (line 1)) Requirement already satisfied (use --upgrade to upgrade): wheel==0.24.0 in ./import-env/lib/python2.7/site-packages (from -r /home/netid/my-env-1-freeze.txt (line 2)) Installing collected packages: nltk Successfully installed nltk-3.1 (import-env)[netid@linux-a2 ~]$
Once you are done with the environment, you may remove the folder (using the examples above, the environment would be located at /home/netid/my-env-1).
Note: If a pip package install is dependent on system libraries which are absent, the install will fail and the libraries will need to be installed in the normal way (usually by the system administrator) before the pip package can be installed.