将Django项目转移到另一台机器

发布于 2025-01-29 05:54:53 字数 1059 浏览 4 评论 0原文

这是一个初学者的问题,但我没有找到答案。我想将我的Django项目从带有Ubuntu 18.04的虚拟机转移到带有Ubuntu 18.04的另一台虚拟机。

示例目录结构

pd_videowebapp/
├── db.sqlite3
├── env
│   ├── bin
│   ├── lib
│   └── pyvenv.cfg
├── manage.py
├── media
│   ├── images
│   └── video
├── mysite
│   ├── core
│   ├── __init__.py
│   ├── __pycache__
│   ├── settings.py
│   ├── static
│   ├── templates
│   ├── urls.py
│   └── wsgi.py
├── Pipfile
├── requirements.txt
└── static
    ├── admin
    ├── style2.css
    └── style3.css

这是env目录中的

。在转移它之前,我将运行

$ pip freeze > requirements.txt

,然后将所有目录结构缩减除db.sqlite3媒体目录。

然后将其解压缩在另一个VM上。

然后将db.sqlite3媒体目录复制到正确的位置。

然后在另一个VM上创建虚拟环境。

然后运行

$ pip install -r requirements.txt

,还是应该在开始时使用env目录复制整个项目?什么更好?我省略了什么吗?还是有更好的方法?

This is a beginner question, but I haven't found the answer. I'd like to transfer my django project from virtual machine with Ubuntu 18.04 to another virtual machine with Ubuntu 18.04.

This is the example directory structure

pd_videowebapp/
├── db.sqlite3
├── env
│   ├── bin
│   ├── lib
│   └── pyvenv.cfg
├── manage.py
├── media
│   ├── images
│   └── video
├── mysite
│   ├── core
│   ├── __init__.py
│   ├── __pycache__
│   ├── settings.py
│   ├── static
│   ├── templates
│   ├── urls.py
│   └── wsgi.py
├── Pipfile
├── requirements.txt
└── static
    ├── admin
    ├── style2.css
    └── style3.css

In env directory there is a Python virtual environment.

Before I transfer it I would run

$ pip freeze > requirements.txt

Then I would zip all the directory structure except for db.sqlite3 and media directory.

Then unzip it on another VM.

Then copy the db.sqlite3 and media directory to the right place.

Then create a virtual environment on another VM.

Then run

$ pip install -r requirements.txt

Or should I rather copy the whole project with env directory in the beginning? What is better? Did I omit something? Or is there a better approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一身骄傲 2025-02-05 05:54:53

还是我应该在开始时使用env> env目录复制整个项目?什么更好?我省略了什么吗?还是有更好的方法?

最好不要复制env目录。排除此目录。

有很多方法可以做到这一点。我建议您使用git。为此:

  1. 从当前项目创建一个git存储库,
  2. 使用适当的.gitignore文件忽略env> env> env目录和其他与环境相关的内容:
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Other stuff
  1. 克隆来自其他VM的项目并配置虚拟此VM 中的环境

更简单:

  1. 在排除env> env目录和其他项目时,将您的整个项目拉开
    手动忽略了东西。

  2. 将zip文件移至其他VM,并在此VM

    中配置虚拟环境

Or should I rather copy the whole project with env directory in the beginning? What is better? Did I omit something? Or is there a better approach?

It is better not to copy the env directory. Exclude this directory.

There are lots of ways to do this. I suggest you use Git. For this:

  1. create a git repository from current project
  2. use proper .gitignore file to ignore env directory and other environment-related stuff:
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Other stuff
  1. clone the project from other VM and config the virtual environment in this VM

Simpler Way:

  1. zip your whole project while excluding env directory and other
    ignored stuffs manually.

  2. move the zip file to other VM and config the virtual environment in this VM

記柔刀 2025-02-05 05:54:53

您可以使用Docker进行更简单的传输

,并且可以使用下面的Dockerfile:

# syntax=docker/dockerfile:1

FROM python:3.9

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

WORKDIR /code

RUN pip install --upgrade pip

COPY requirements.txt /code/

RUN pip install -r requirements.txt

#COPY daemon.json /etc/docker/daemon.json

COPY . /code/

EXPOSE 80

您可以在此处找到有关django应用程序的更多信息。 noreferrer“> https://docs.docker.com/samples/django/

希望您对此有所帮助!

You can use docker for a simpler transfer

and you can use the Dockerfile below:

# syntax=docker/dockerfile:1

FROM python:3.9

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

WORKDIR /code

RUN pip install --upgrade pip

COPY requirements.txt /code/

RUN pip install -r requirements.txt

#COPY daemon.json /etc/docker/daemon.json

COPY . /code/

EXPOSE 80

you can find more on dockerizing a django app here https://docs.docker.com/samples/django/

Hope you find this helpful!

居里长安 2025-02-05 05:54:53

每个答案都有其他答案。我将尝试将它们汇编成一个可行且合理的计划。

请勿复制虚拟环境目录

Python虚拟环境目录已优化以使用链接而不是副本,因此,如果您在单个机器上创建类似的虚拟环境,则不会用副本膨胀磁盘相同的PIP软件包和Python文件。

复制环境破坏此模型。它可能行不通或行为不当。

使用git和.gitignore来指定波动性资源

如果您使用git管理项目,则绝对应该有一个.gitignore说哪些资源是自动生成的,用户特定于用户特定的,挥发性等,不应成为共享回购的一部分。

您应该像@hamraa所建议的那样设置它,然后再继续下一步。

使用git搬迁项目

假设您在这两台机器上都有互联网连接,为什么您必须拉链任何东西并移动它?

如果这两台机器都可以访问同一GIT服务器,则应将更改推向全局存储库,然后将其从其他VM中提取。

如果不是这种情况,但是目标VM具有与源计算机的SSH连接,则可以致电:

git clone ssh://<username>@<hostname>:/<path-to-repo>

下创建可移动的捆绑包

使用Git-bundle,如果您的repo 在没有网络连接的情况 计算机,您可以使用一个文件轻松地使用单个文件来移动存储库的酷git功能。

该命令很简单:

git bundle create app.bundle <commit/tag/branch>

然后您可以将此捆绑包移至任何其他机器并使用:

git clone app.bundle

考虑使用Docker

我不会反对Docker,我喜欢Docker。但是(!)我认为 不是解决方案。使用良好的Bure-up脚本,您可以使用非常易于使用的Git Repo,可以快速,轻松地进行设置。很少有一个正确的方法。我认为这不是其中之一。

Each and every answer has an element that is missing from the others. I'll try and compile all of them into one actionable and reasonable plan.

Do not copy virtual env directories

The Python virtual environment directory is optimized to use links rather than copies, so that if you create similar virtual environments on the single machine, you won't bloat up your disk with copies of the same pip packages and Python files.

Copying an environment break this model. It might not work or misbehave.

Use git and .gitignore to specify volatile resources

If you manage your project using git, you should definitely have a .gitignore that says which resources are auto-generated, user-specific, volatile, etc. and should not be part of the shared repo.

You should set it up like @hamraa suggested before continuing to the next step.

Use git to relocate the project

Assuming you have an internet connection on both machines, why do you have to zip anything and move it?

If both machines have access to the same Git server, you should just push your changes to the global repo, and pull those from the other VM.

If that's not the case, but the target VM has an SSH connection to the source machine, you could just call:

git clone ssh://<username>@<hostname>:/<path-to-repo>

Use git-bundle to create a moveable bundle from your repo

If there's no network connection between the machines, you can use a cool Git feature of moving repos easily using a single file.

The command is as simple as:

git bundle create app.bundle <commit/tag/branch>

You can then move this bundle to any other machine and use:

git clone app.bundle

Consider using Docker

I won't argue against Docker, I love Docker. But(!) I don't think that has to be the solution. Using good bring-up scripts, you could just have a very easy to use Git repo that you can setup quickly and easily. Rarely there are cases where's there's a single right way. I don't think this is one of them.

风吹雨成花 2025-02-05 05:54:53

您正在思考正确的方式。您可以在没有env目录的情况下压缩项目。

在新系统中创建虚拟环境:假设您已经安装了python3-pippython3-dev等系统通过从终端进入项目DIR并执行这些命令(如您所知) -

# install virtual env
pip install virtualenv

# create virtual env
virtualenv -p /usr/bin/python3 venv

# activate virtual env
source venv/bin/activate

# install project dependencies
pip install -r requirements.txt

然后您都很好。此外,您可以创建一个远程存储库(例如在github上),并通过.gitignore文件忽略虚拟环境dir env> env]

如果您正在寻找不需要重新配置的解决方案新系统,我强烈建议使用docker

You're thinking the right way. You may compress the project without the env directory.

Create a virtual environment in the new system: assuming that you have already installed python3-pip, python3-dev etc. as required, and then set up the project in your new system by going to the project dir from the terminal and performing these commands (as you know) -

# install virtual env
pip install virtualenv

# create virtual env
virtualenv -p /usr/bin/python3 venv

# activate virtual env
source venv/bin/activate

# install project dependencies
pip install -r requirements.txt

Then you're all good. Besides, you may create a remote repository (e.g. on Github) and ignore the virtual environment dir env via the .gitignore file]

If you're looking for a solution that doesn't require reconfiguring the new system, I highly recommend using Docker

云柯 2025-02-05 05:54:53

由于您正在从一个VM转移到另一个VM,因此假设您在AWS上,则可以使用ami创建当前实例(VM)的快照,然后启动带有保存的新实例(VM) ami。这样做的优点是它将保留您当前的所有设置,代码库和数据。您不必开始从头开始配置新机器。

https://docs.aws.amazon。 com/awsec2/laste/userguide/create-an-ami-ebs.html

Since you are transferring from one VM to another, assuming you are on AWS you can use the AMI to create a snapshot of your current instance (VM) and then launch a new instance (VM) with the saved AMI. The advantage of this is that it will retain all your current settings, codebase, and data. You don't have to start configuring the new machine from scratch.

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/creating-an-ami-ebs.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文