将Django项目转移到另一台机器
这是一个初学者的问题,但我没有找到答案。我想将我的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
最好不要复制
env
目录。排除此目录。有很多方法可以做到这一点。我建议您使用
git
。为此:.gitignore
文件忽略env> env> env
目录和其他与环境相关的内容:更简单:
在排除
env> env
目录和其他项目时,将您的整个项目拉开手动忽略了东西。
将zip文件移至其他VM,并在此VM
中配置虚拟环境
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:.gitignore
file to ignoreenv
directory and other environment-related stuff:Simpler Way:
zip your whole project while excluding
env
directory and otherignored stuffs manually.
move the zip file to other VM and config the virtual environment in this VM
您可以使用Docker进行更简单的传输
,并且可以使用下面的Dockerfile:
您可以在此处找到有关django应用程序的更多信息。 noreferrer“> https://docs.docker.com/samples/django/
希望您对此有所帮助!
You can use docker for a simpler transfer
and you can use the Dockerfile below:
you can find more on dockerizing a django app here https://docs.docker.com/samples/django/
Hope you find this helpful!
每个答案都有其他答案。我将尝试将它们汇编成一个可行且合理的计划。
请勿复制虚拟环境目录
Python虚拟环境目录已优化以使用链接而不是副本,因此,如果您在单个机器上创建类似的虚拟环境,则不会用副本膨胀磁盘相同的PIP软件包和Python文件。
复制环境破坏此模型。它可能行不通或行为不当。
使用git和.gitignore来指定波动性资源
如果您使用git管理项目,则绝对应该有一个
.gitignore
说哪些资源是自动生成的,用户特定于用户特定的,挥发性等,不应成为共享回购的一部分。您应该像@hamraa所建议的那样设置它,然后再继续下一步。
使用git搬迁项目
假设您在这两台机器上都有互联网连接,为什么您必须拉链任何东西并移动它?
如果这两台机器都可以访问同一GIT服务器,则应将更改推向全局存储库,然后将其从其他VM中提取。
如果不是这种情况,但是目标VM具有与源计算机的SSH连接,则可以致电:
下创建可移动的捆绑包
使用Git-bundle,如果您的repo 在没有网络连接的情况 计算机,您可以使用一个文件轻松地使用单个文件来移动存储库的酷git功能。
该命令很简单:
然后您可以将此捆绑包移至任何其他机器并使用:
考虑使用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:
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:
You can then move this bundle to any other machine and use:
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.
您正在思考正确的方式。您可以在没有
env
目录的情况下压缩项目。在新系统中创建虚拟环境:假设您已经安装了
python3-pip
,python3-dev
等系统通过从终端进入项目DIR并执行这些命令(如您所知) -然后您都很好。此外,您可以创建一个远程存储库(例如在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) -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
由于您正在从一个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 savedAMI
. 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