
Understanding virtual environments
While working with Python, you will probably use a multitude of libraries or packages. The virtual environment called venv is the first and most immediate way of making a working setup that is easy to reproduce.
From Python 3.3, the venv module is Python's built-in module, meaning you don't need to install any external components.
To create an environment in an automated way, it's necessary to create a list with all the libraries you want to install. Pip has a very simple way of defining this list, and it's enough to create a .txt file and specify one library per line.
To create the environment, you will need to perform the following steps:
- Install Python 3.7.
- Create a new environment called dl_venv_pip by using the following command:
python3.7 -m venv dl_venv_pip
- Specify the required libraries in a file that we will call requirements.txt
,
with the following content:
numpy==1.15.4
scipy==1.1.0
pandas==0.23.4
tensorboard==1.12.1
tensorflow==1.11.0
scipy==1.1.0
scikit-learn==0.20.1
Keras==2.2.4
- It's possible to now install all the required libraries with this command:
pip install -r /path/to/requirements/requirements.txt
- Now we can activate our environment by typing in the following command:
source dl_venv_pip/bin/activate
Once the environment is activated, all your calls to the Python interpreter will be redirected to the environment's interpreter. It's a quick and easy way to distribute the requirements, but it's still possible to have compatibility issues due to different operating systems, and also it might require some time to install all libraries as many data science projects rely on many of them.