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
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.
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
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