Migrating a Project in a Virtual Environment to Python
There are often situations when you need to move the original project to a new folder, for example, to a new server. Let's figure out how to do this with a virtual environment in Python.
First, let's create a new folder new_project_ven
, into which we'll move the project from project_ven
:
- /project_venv/
- ...
- /project1/
- main.py
- /new_project_venv/
Next, we copy all installed packages from the virtual environment project_ven
. This is done using the following command:
python -m pip freeze > req.txt
To the left of the word freeze
, there is an angle bracket and the name of the file that will contain information about the packages. In our case, such a file is called req.txt
, but its name can be anything. After executing the command, a new file will appear in the project folder:
- /project_venv/
- ...
- /project1/
- main.py
- req.txt
Next, you need to copy the folder project1
, which already contains two files: main.py
and req.txt
. Then paste it into the folder new_project_venv
:
- /project_venv/
- ...
- /project1/
- main.py
- req.txt
- /new_project_venv/
- /project1/
- main.py
- req.txt
- /project1/
Now we exit the current virtual environment:
deactivate
Then in the console you need to go to the folder new_project_venv/project1
. After that, you should create a new virtual environment for the new project:
python -m venv ..
Next, we activate our new environment:
..\Scripts\activate # for Windows
source ../bin/activate # for Linux
After that, you will see the name of the new virtual environment in the console:
(new_project_venv)
Now we need to restore the packages from the original project. To do this, use the following command:
pip install -r req.txt
After successful installation of the library, the following message will be displayed:
Successfully installed numpy-1.26.4
Transfer the project you created to a new virtual environment.
Restore all libraries from the original folder in the new project. Check the operation of the installed libraries.