Create a Python Virtual Environment with Built-In venv Module
Data Scientists or even Python Software Engineers commonly need to use python virtual environments for using dependencies, apart from those installed in their system.
Python provides the option to create virtual environments without installing any software or additional tools.
Let's get started with the following steps:
- In order to check the current packages installed with pip, enter the command in cmd terminal :
pip list
This will show all active dependencies with their versions.
2. In order to create the new virtual environment enter the command:
python -m venv project_env
This will create a new virtual environment named “project_env”.
3. Once the environment is created, you can activate the environment using the command:
project_env\Scripts\activate.bat
This will activate the virtual environment and this can be seen in the terminal as shown in the following figure:
The new environment is now activated. Let us check the dependencies installed in venv project_env.
4. In order to deactivate simply enter: deactivate
5. As a Software Developer it is a good practice to include a requirements.txt file for a new person to install all dependencies. This can be easily done with the command: pip freeze
The output can be copied and pasted in the requirements.txt file.
6. In order to delete the virtual environment simply enter the command: rmdir project_env /s
The /s ensure to delete the venv even in case the directory is empty.
Hope this was informative.
Feel free to contact me here for doubts and here for more of my articles.