Transfer a django project to another machine

Viewed 231

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?

5 Answers

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

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!

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

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.

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

Related