在Ubuntu上部署Python项目可以通过多种方法实现,以下是一些常见的部署步骤:
sudo apt update
sudo apt install python3 python3-pip
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
python your_script.py
sudo apt update
sudo apt install docker.io
# 使用官方的Python基础镜像
FROM python:3.8
# 设置工作目录
WORKDIR /app
# 将当前目录下的所有文件复制到容器的/app目录
COPY . /app
# 安装项目所需的依赖包
RUN pip install --no-cache-dir -r requirements.txt
# 暴露端口,如果有需要的话
EXPOSE 8080
# 运行Python应用
CMD ["python", "your_script.py"]
docker build -t your_image_name .
docker run -p 8080:8080 your_image_name
[Unit]
Description=My Python Service
After=network.target
[Service]
User=your_username
ExecStart=/home/your_username/miniconda3/envs/my_project_env/bin/python /path/to/your_script.py
Restart=always
RestartSec=10
Environment="PATH=/home/your_username/miniconda3/envs/my_project_env/bin/:$PATH"
[Install]
WantedBy=multi-user.target
sudo systemctl enable my_service.service
sudo systemctl start my_service.service
sudo systemctl status my_service.service
以上步骤涵盖了在Ubuntu上部署Python项目的基本流程,包括使用虚拟环境、Docker容器以及systemd服务。根据项目的具体需求,可以选择适合的部署方式。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:Python在Ubuntu上如何部署应用