...
Setting Up a Virtual Environment
You’ll want to first get an interactive session, especially if you plan on installing scientific packages. compute-*-*
is a placeholder for whatever compute node you land on after requesting your interactive session.
Setup
...
Code Block |
---|
mfk8@login01:~$ srun --pty -p interactive -t 0-1:00 --mem=1G bash # adjust srun parameters accordingly as needed; you may need to request more memory if installing large packages mfk8@compute-*-*:~$ module load gcc/9.2.0 mfk8@login01mfk8@compute-*-*:~$ module avail python mfk8@login01mfk8@compute-*-*:~$ module load python/3.8.12 # or whichever version you'd like here mfk8@login01mfk8@compute-*-*:~$ which virtualenv mfk8@login01mfk8@compute-*-*:~$ virtualenv nameyourenvhere # Please read on before executing this command as-is |
...
If you'd like to use the packages that are pre-installed in the Python module, include the --system-site-packages
flag when creating the virtual environment:
Code Block |
---|
mfk8@login01mfk8@compute-*-*:~$ virtualenv nameyourenvhere --system-site-packages |
...
To uninstall a virtual environment, simply rm -rf
the folder containing the directory:
Code Block |
---|
mfk8@login01mfk8@compute-*-*:~$ rm -rf nameyourenvhere |
...
To begin using the virtual environment, it needs to be activated. For a virtual environment located at ~/venv
:
...
Code Block |
---|
mfk8@compute-*-*:~$ source venv/bin/activate |
Your prompt will now look (something like) this:
Code Block |
---|
(venv)mfk8@login01mfk8@compute-*-*:~$ |
From here you an confirm that you're using the python associated with your virtual environment:
Code Block |
---|
(venv)mfk8@login01mfk8@compute-*-*:~$ which python3 ~/venv/bin/python3 |
...
To deactivate the environment, simply type deactivate
, to see the corresponding change to your terminal prompt:
Code Block |
---|
(venv)mfk8@login01mfk8@compute-*-*:~$ deactivate mfk8@login01:~$ |
...
To install something, simply type:
Code Block |
---|
(venv)mfk8@login01mfk8@compute-*-*:~$ pip3 install nameofpackage |
...
Here is an example of loading the desired version of Python on O2, setting up a virtual environment and activating it, installing a package to this personal environment, running a Python script, and deactivating the virtual environment (2.x chosen here arbitrarily):
...
language | bash |
---|
...
.x chosen here arbitrarily):
Code Block | ||
---|---|---|
| ||
mfk8@login01:~$ srun --pty -p interactive -t 0-1:00 --mem=1G bash ... mfk8@compute-*-*:~$ which python /usr/bin/python mfk8@login01mfk8@compute-*-*:~$ module load gcc/6.2.0 mfk8@login01mfk8@compute-*-*:~$ module avail python --------------- /n/app/lmod/lmod/modulefiles/Compiler/gcc/6.2.0 ---------------- python/2.7.12 (E) python/3.6.0 (E,D) python/3.7.4 Where: E: Experimental D: Default Module Use "module spider" to find all possible modules. Use "module keyword key1 key2 ..." to search for all possible modules matching any of the "keys". mfk8@login01mfk8@compute-*-*:~$ module load python/3.7.4 mfk8@login014 mfk8@compute-*-*:~$ which python3 /n/app/python/3.7.4/bin/python3 mfk8@login01mfk8@compute-*-*:~$ which virtualenv /n/app/python/2.7.12/bin/virtualenv mfk8@login01mfk8@compute-*-*:~$ mkdir mypythonfolder && cd mypythonfolder mfk8@login01mfk8@compute-*-*:~/mypythonfolder$ virtualenv myvirtualenv (truncated) mfk8@login01mfk8@compute-*-*:~/mypythonfolder$ source myvirtualenv/bin/activate (myvirtualenv)mfk8@login01mfk8@lcompute-*-*:~/mypythonfolder$ pip3 install numpy (truncated) (myvirtualenv)mfk8@login01mfk8@compute-*-*:~/mypythonfolder$ echo "print 'hello world'" > myscript.py (myvirtualenv)mfk8@login01mfk8@compute-*-*:~/mypythonfolder$ python3 myscript.py hello world (myvirtualenv)mfk8@login01mfk8@compute-*-*:~/mypythonfolder$ deactivate mfk8@login01mfk8@compute-*-*:~/mypythonfolder$ |
Troubleshooting
...