Background Image
Table of Contents Table of Contents
Previous Page  170 / 478 Next Page
Information
Show Menu
Previous Page 170 / 478 Next Page
Page Background elspeth@server

:$

mkdir -p ~/sites/$SITENAME/static

elspeth@server

:$

mkdir -p ~/sites/$SITENAME/virtualenv

# you should replace the URL in the next line with the URL for your own repo

elspeth@server

:$

git clone

https://github.com/hjwp/book-example.git

\

~/sites/$SITENAME/source

Resolving deltas: 100% [...]

A bash variable defined using

export

only lasts as long as that con‐

sole session. If you log out of the server and log back in again, you’ll

need to redefine it. It’s devious because Bash won’t error, it will just

substitute the empty string for the variable, which will lead to weird

results … if in doubt, do a quick

echo $SITENAME

.

Now we’ve got the site installed, let’s just try running the dev server—this is a smoke

test, to see if all the moving parts are connected:

elspeth@server

:$ $

cd ~/sites/$SITENAME/source

$

python3 manage.py runserver

Traceback (most recent call last):

File "manage.py", line 8, in <module>

from django.core.management import execute_from_command_line

ImportError: No module named django.core.management

Ah. Django isn’t installed on the server.

Creating a Virtualenv

We could install it at this point, but that would leave us with a problem: if we ever wanted

to upgrade Django when a new version comes out, it would be impossible to test the

staging site with a different version from live. Similarly, if there are other users on the

server, we’d all be forced to use the same version of Django.

The solution is a “virtualenv”—a neat way of having different versions of Python pack‐

ages installed in different places, in their own “virtual environments”.

Let’s try it out locally, on our own PC first:

$

pip3 install virtualenv

# will need a sudo on linux/macos.

We’ll follow the same folder structure as we’re planning for the server:

$

virtualenv --python=python3 ../virtualenv

$

ls ../virtualenv/

bin include lib

That will create a folder at

../virtualenv

which will contain its own copy of Python and

pip

, as well as a location to install Python packages to. It’s a self-contained “virtual”

Python environment. To start using it, we run a script called

activate

, whichwill change

the system path and the Python path in such a way as to use the virtualenv’s executables

and packages:

142

|

Chapter 8: Testing Deployment Using a Staging Site