In some cases you have to replicate a Python project on a computer without internet connection. In these cases you must not simply copy the venv folder.
Requirements for this process: the project, a memory mass storage, another computer with internet connection.
First of all, you have to to install Python on the offline machine. Now let’s go deeper into the venv replication process.
Take a look to the requirements.txt file of the project where all the dependencies are declared. Keep in mind: this file might not contain all the recursive dependencies.
Now we are going to download all the dependencies: jump into the root of the project and launch the following command (the first one for a single dependency, the second one for the entire list of requirements) from the terminal:
pip download <package-name/version>
pip download -r ./path/to/requirements.txt -d ./download/folder/
For example:
pip download itsdangerous==2.0.1
Collecting itsdangerous==2.0.1
Using cached itsdangerous-2.0.1-py3-none-any.whl (18 kB)
Saved ./itsdangerous-2.0.1-py3-none-any.whl
Successfully downloaded itsdangerous
We can see that a wheel file or a compressed package has been now downloaded with all its recursive dependencies.
Repeat this process for the packages declared in the requirements.txt file and you will have a pool of libraries, ready to be copied into the new project.
If you have downloaded all the packages is now time to create a new fresh virtual envoronment into the project in the offline computer:
python -m venv ./venv/
Copy all the python libraries downloaded into a directory inside the project and jump inside.
To install a single wheel:
pip install xyz.wheel
To install an unpacked dependency:
pip install ./my/dependency/
To install all the dependencies automatically using the requirements.txt file:
pip install -r requirements.txt --no-index --find-links file:./path/to/dependencies/folder/
If you need to download for a different platform, you should use the extra option:
--platform
Cheers
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.