SoFunction
Updated on 2024-10-26

Python Dependency Package Migration to Disconnected Environment Operation

Scene:Due to the lack of performance of your own computer A, you need to move to a high-performance host B to run your python program, but that host cannot connect to the Internet.

Question:I have created a virtual environment on PC A, installed the dependency packages and debugged the program to make it work. However, when I copy and paste the folder where the virtual environment is located to host B, it does not work.

Environment: Windows, Python 3.6

Solution:

1. host A (virtual environment activation) pip freeze > Record the Python dependency packages installed in the virtual environment to a file.

2. Host A (virtual environment activation) pip download -r -d packages Save the installed Python dependencies to the packages directory.

3. Host B Install python 3.6 on the disconnected host.

4. Host B Create a virtual environment under the project and activate it. Also copy and paste the whls folder and into the project directory.

5. Host B (virtual environment activation) pip install --no-index --find-links=packages -r Installs the dependencies in the virtual environment on host B.

Additional knowledge:The python project exports the required dependency libraries or libraries for the entire environment

Using pip freeze

pip freeze >

This command installs all third-party packages in the environment, which is generally easier to use in a virtual environment.

Installation commands

pip install -r

Using pipreqs

If you want to organize the third-party libraries used by a project in a normal environment you can use pipreqs

First Step Installation

pip install pipreqs

It's also easy to use pipreqs pathname

This goes directly to the project root directory, so it's . /

pipreqs ./

This directly generates the third-party libraries used in the project.

If an error is reported

File "c:\users\devtao\appdata\local\programs\python\python36-32\lib\site-packages\pipreqs\", line 341, in init
extra_ignore_dirs=extra_ignore_dirs)
File "c:\users\devtao\appdata\local\programs\python\python36-32\lib\site-packages\pipreqs\", line 75, in get_all_imports
contents = ()
UnicodeDecodeError: 'gbk' codec can't decode byte 0xa6 in position 186: illegal multibyte sequence

You can change the encoding='utf-8' around line 74 in the installer.

Above this Python dependency package migration to disconnected environment operation is all that I have shared with you, I hope to give you a reference, and I hope you will support me more.