SoFunction
Updated on 2025-03-05

Implementation of Python project packaging and deployment to server

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 usedpipInstall dependencies and passsetuptoolsCreate executable scripts.

  • Install the packaging tool
    First, make sure that your environment is installedsetuptoolsandwheel
pip install setuptools wheel
  • createCreate a project root directoryThe 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_nameyour_script_nameyour_package.moduleandmain_functionInformation 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 willdistFound in the directory.and.whldocument.

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 usenohupand&

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_nameFor 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!