|
| 1 | +# Virtual Environments in Python |
| 2 | +[The Python Tutorial](https://docs.python.org/3/tutorial/venv.html) |
| 3 | + |
| 4 | +Python applications will often use packages and modules that don’t come as part of the standard library. Applications will sometimes need a specific version of a library, because the application may require that a particular bug has been fixed or the application may be written using an obsolete version of the library’s interface. |
| 5 | + |
| 6 | +This means it may not be possible for one Python installation to meet the requirements of every application. If application A needs version 1.0 of a particular module but application B needs version 2.0, then the requirements are in conflict and installing either version 1.0 or 2.0 will leave one application unable to run. |
| 7 | + |
| 8 | +The solution for this problem is to create a virtual environment, a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages. |
| 9 | + |
| 10 | +## venv |
| 11 | +To create a virtual environment, decide upon a directory where you want to place it, and run the venv module as a script with the directory path: |
| 12 | +```bash |
| 13 | +python3 -m venv tutorial-env |
| 14 | +``` |
| 15 | + |
| 16 | +This will create the `tutorial-env` directory if it doesn’t exist, and also create directories inside it containing a copy of the Python interpreter, the standard library, and various supporting files. Once you’ve created a virtual environment, you may activate it by: |
| 17 | +```bash |
| 18 | +source tutorial-env/bin/activate |
| 19 | +``` |
| 20 | + |
| 21 | +We can use the following Bash scripts to create `tutorial-env` if it does not exist and activate it and install modules: |
| 22 | +```bash |
| 23 | +#!/bin/bash |
| 24 | + |
| 25 | +if [ ! -d tutorial-env ]; then |
| 26 | + python3 -m venv tutorial-env |
| 27 | +fi |
| 28 | + |
| 29 | +source tutorial-env/bin/activate |
| 30 | + |
| 31 | +pip install --upgrade pip |
| 32 | +pip install redis |
| 33 | +pip install gitpython |
| 34 | +``` |
| 35 | + |
| 36 | +As a shortkey, you might use `.` instead `source`. To exit from a virtual environment run `deactivate`. |
| 37 | + |
| 38 | +## Miniconda |
| 39 | +To use use Conda as an environment management system, you need to install Miniconda (or Anaconda) and use `conda create` to make a new environment by: |
| 40 | +```bash |
| 41 | +conda create --name <env_name> <pkg name> <pkg name> ... |
| 42 | +``` |
| 43 | + |
| 44 | +You also can use `--yes` flag to set up the environment without a question and use `--prefix <env_path>` to setup the environment in the path that you want (note that you can not use `--prefix` and `--name` at the same time). To see list of environments, use the following: |
| 45 | +```bash |
| 46 | +conda env list |
| 47 | +``` |
| 48 | + |
| 49 | +You might also use `conda info --envs` to see list of envs. To activate/deactivate an environment use the following: |
| 50 | +```bash |
| 51 | +conda activate <env_name or env_path> |
| 52 | +conda deactivate |
| 53 | +``` |
| 54 | + |
| 55 | +Note that, for conda versions prior to 4.6 you need to use `source` instead of `conda` in the above commands. Now, you can use the following to update `conda` and install new libraries and software: |
| 56 | +```bash |
| 57 | +conda update conda |
| 58 | +conda install <pkg name> |
| 59 | +``` |
| 60 | + |
| 61 | +To remove a package you can use: |
| 62 | +```bash |
| 63 | +conda remove <pkg name> |
| 64 | +``` |
| 65 | + |
| 66 | +And to remove cache files use: |
| 67 | +```bash |
| 68 | +conda clean --all |
| 69 | +``` |
| 70 | + |
| 71 | +**Note**: before removing packages or caches, make sure you are in the right virtual environment. You can use `conda info` and `conda env list` to find your current environment and use `source activate <env name or path>` or `source deactivate` to go to the rigth env. |
| 72 | + |
| 73 | +To remove a virtual environment you can use: |
| 74 | +```bash |
| 75 | +conda env remove --name <env_name> or --prefix <env_path> |
| 76 | +``` |
| 77 | +**Note**: you cannot remove an environment when it is activate. Note that sometime you should `deactivate` multiple times, if you `activate` more than once. |
| 78 | + |
| 79 | +In a **HPC** cluster system, first you need to load `miniconda3` module to be able to use `conda`. You can use the following as a template to build your new environment: |
| 80 | +```bash |
| 81 | +module load miniconda3 |
| 82 | + |
| 83 | +if [ ! -d yourlocal_env ]; then |
| 84 | +conda create --yes --prefix ./yourlocal_env <pkg name> <pkg name> ... |
| 85 | +fi |
| 86 | +source activate ./yourlocal_env |
| 87 | +``` |
0 commit comments