Importing and exporting environments
What this is about
Setting up virtual environments (VENV) allows several Python versions to be used in parallel and dependencies to be isolated completely, preventing version conflicts. Recording those dependencies in a requirements.txt file makes the installation process reproducible at any time and easy to hand on to other systems or to team members. Every project thereby gets an independent, controlled environment, which makes both development and operation lastingly stable.
-
Step 1 of 4 Open the command prompt (CMD)
Drücken Sie Win + R, geben Sie
cmdand confirm with Enter. -
Step 2 of 4 Activate the existing environment Export from the old environment
Change into the Scripts folder of the virtual environment you want. Then run the following command to activate the virtual environment:
cd "C:\Users\<your User>\pythonEnv\myFirstEnv\Scripts"activateOnce activated, the command line prompt shows the name of the environment in brackets, for example:
(myFirstEnv) C:\Users\<your User>\pythonEnv\myFirstEnv\Scripts>This indicates that the virtual environment has been activated successfully.
-
Step 3 of 4 Create the requirements file Export from the old environment
Run the following command to create an installation template named my_requirements.txt for the virtual environment you activated:
pip freeze > /pfad/zu/Ihrem/Speicherort/my_requirements.txt -
Step 4 of 4 Install the packages in a new environment Import into the new environment
Create a new Python environment and follow the ‚Virtual environment (venv)‘ module to install it at the intended location.
If several Python versions are installed on your machine and you want to create different environments with different Python versions, use the following command (the example below uses Python version 3.11):
py -3.11 -m venv /pfad/zu/Ihrer/env/venv_nameThen activate the virtual environment as described in step 2 and install all required packages from the file my_requirements.txt with the following command:
Download example package list The package list from the script and screencast can be downloaded here and then used with pip install -r.requirements_onboarding.txtpip install -r /pfad/zu/Ihrer/my_requirements.txtAll packages should now be installed in your newly created virtual environment. Check this by entering the following command on the command line:
pip list