1. Preparation
- Development Environment: This article takes PyCharm as an example, and the same applies to other IDEs or text editors.
- Server: This article takes Ubuntu as an example, and the same applies to other Linux systems.
- Python project: Development has been completed and can be run locally.
2. Project packaging
Python project packaging is usually usedpip
Install dependencies and passsetuptools
Create executable scripts.
- Install the packaging tool
First, make sure that your environment is installedsetuptools
andwheel
:
pip install setuptools wheel
- create
Create a project root directory
The file, the content is as follows:
from setuptools import setup, find_packages setup( name='your_project_name', version='0.1.0', packages=find_packages(), install_requires=[ # Fill in the package that the project depends on here, for example 'Flask==1.1.2', 'requests==2.25.1', ], entry_points={ 'console_scripts': [ 'your_script_name=your_package.module:main_function', ], }, )
replaceyour_project_name
、your_script_name
、your_package.module
andmain_function
Information for your project.
3. Packaging the project
In the command line, enter the project root directory and execute the following command to package:
python sdist bdist_wheel
After the package is complete, you willdist
Found in the directory.
and.whl
document.
3. Deploy to the server
- Upload packaged files to the server
Use SCP command to upload the packaged files to the server:
scp dist/your_project_name-0.1. user@your_server_ip:/path/to/deploy
- Installation project dependencies
Connect to the server and install project dependencies:
ssh user@your_server_ip cd /path/to/deploy pip install your_project_name-0.1.
Or, if you uploaded.
Files can be installed using the following command:
pip install your_project_name-0.1.
- Run the project
After the installation is complete, you can run your script directly:
your_script_name
If you need to run the background, you can usenohup
and&
:
nohup your_script_name &
- View the run log
View the run log:
tail -f
4. Set up service start-up
To make the project run automatically when the server starts, a system service can be created.
- Create a service file
sudo nano /etc/systemd/system/your_project_name.service
Add the following:
[Unit] Description=My Python Project Service After= [Service] User=user ExecStart=/usr/bin/python /path/to/your_script_name Restart=on-failure [Install] WantedBy=
replaceuser
、/path/to/your_script_name
For your username and script path.
Start and enable the service to start automatically
sudo systemctl start your_project_name sudo systemctl enable your_project_name
Now your Python project should have been successfully deployed to the server and configured as a self-start service. If you need to restart or stop the service, you can use the following command:
sudo systemctl restart your_project_name sudo systemctl stop your_project_name
Through the above steps, your Python project can be run stably on the server.
This is the end of this article about the implementation of Python project packaging and deployment to the server. For more related contents of Python project packaging and deployment to the server, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!