Python comes with an ability to create virtual environments.
A virtual environment is a sandboxed areas of memory, where modules can be installed without affecting the modules installed and available in other environments.
This allows each project to have its own isolated memory area with its own set of module dependencies, which may be different from that of other projects.
This command creates a new virtual environment with the name .env
:
python3 -m venv .env
To activate the virtual environment named .env
...
On Windows, run:
.env\Scripts\activate.bat
On Mac, run:
source .env/bin/activate
The install a new module into the currently-active virtual environment, use pip
, the default Python "package manager" - software that takes care of installing the correct version of any module into your in the correct place for the current environment.
For example, to install the emojis
module:
pip install emojis
The following command will output a list of all modules installed into the current environment, along with their version numbers.
pip freeze
To save a list of all modules in the current environment into a file named requirements.txt
...
pip freeze > requirements.txt
Saving a list of module dependencies into a file like this allows you to easily recreate the virtual environment with the necessary modules on a different computeer.
If setting up an existing project from scratch, conventional practice is to first creeate a new virtual environment. Then install all the modules listed in the file named requirements.txt
by running the following command.
pip install -r requirements.txt