Linux python environment migration, [python] python project uses virtual environment to migrate on windows, linux, and non-networked linux servers. Summary...

Recently needed to deploy a python service on a linux server without internet. Checked the relevant information, the practice is feasible, the summary is as follows

Install python on a linux machine that can connect to the Internet

On the python official website, download the specified version of python

53af8a6ca787a7b53f98af1294947af7.png

Click the specified version link to find the version applicable to linux, mainly to be consistent with the linux version of the linux server without the Internet.

c40ba228864b454411a3aa06bf74a6e3.png

Copy Python-3.7.0.tar.xz to the linux machine

decompress

xz -d Python-3.7.0.tar.xz

tar xvfPython-3.7.0.tar

Go to the Python-3.7.0 directory

run ./configure

run make

run make test

run sudo make install

Generate the dependency package list file of the original virtual environment project on the windows machine

If pycharm is not in a virtual environment, running the project will automatically start the virtual environment

In the project directory virtual environment

pip3 freeze > requirements.txt

Copy the entire project along with the requirements.txt file to the linux machine

Run the generated virtual environment on a linux machine that can connect to the Internet

Find the installed python3.7.0

$ whereis python

$ whereis python3.7.0

Generate virtual environment

virtualenv -p /usr/local/bin/python3.7 venv2

venv2 is the new virtual environment directory

Enter the virtual environment

source venv2/bin/activate

7f1fd1a5e0c0ed8f6d72a6c178e02571.png

Install a package in a virtual environment

pip3 install -r requirement.txt

For the convenience of transplantation, all the required packages are downloaded here, and the virtualenv creation tool virtualenv is downloaded here by the way.

pip3 download -r requirement.txt

pip3 download virtualenv

After that some .whl files will be generated

Porting virtual environments on machines that cannot be connected to the Internet

Copy the above items, as well as the requirement.txt and .whl files to a machine without Internet access.

*************************************************************

For example, connecting to a docker image

Terminal1:

docker run --entrypoint=bash -it ca88

Terminal2:

docker ps finds the corresponding number

copy file

docker cp file_name.tar f85f:/home/service

*************************************************************

On this machine,

via whereis python3

Find the python3 directory

Generate virtual environment

virtualenv -p /usr/local/bin/python3.7 venv3

Enter the virtual environment

source venv3/bin/activate

Installation package

pip3 install -r requirement.txt

Exit the virtual environment

Deactivate

The running process configures environment variables and runs in the project directory

export PYTHONPATH=`pwd`

The startup script can be written as

#!/bin/bash

work_path=$(dirname $0)

cd $work_path

export PYTHONPATH=$work_path

"venv3/bin/python3.7" src/data_preprocess/b03_http_server.py

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324134201&siteId=291194637