Skip to content

Instantly share code, notes, and snippets.

@christophergraber
Last active November 13, 2021 20:22
Show Gist options
  • Save christophergraber/c07fccc7a8bb325c43edb3fb0608c914 to your computer and use it in GitHub Desktop.
Save christophergraber/c07fccc7a8bb325c43edb3fb0608c914 to your computer and use it in GitHub Desktop.

Virtual Environment Setup with Django on Mac

Note: This assumes python3 is already installed

To initially get virtualenv set up:

$ sudo pip3 install virtualenv

And then:

$ mkdir ~/newproject  &&  cd newproject

Create a virtual environment within the project directory by typing:

$ python3 -m venv myenv

Move into the venv:

$ cd myenv

To install packages into the isolated environment, you must activate it by typing:

$ source bin/activate

Now install Django in the venv with:

$ pip3 install django

Might get a note like:

WARNING: You are using pip version 21.2.3; however, version 21.3.1 is available. You should consider upgrading via the '/Users/chrisg/newproject/newenv/bin/python3 -m pip install --upgrade pip' command.

Can verify Django installation with:

$ django-admin --version

Alternately:

$ python3 -m django --version

Start a Django project in the venv:

$ django-admin startproject project
$ cd project (you will see manage.py in there)

Also you can create an individual app inside the project:

$ django-admin startapp blog

Start Django:

$ python3 manage.py runserver

Migrations:

$ python3 manage.py makemigrations
$ python3 manage.py migrate

To make an localhost:8080/admin/ login:

$ python3 manage.py createsuperuser

To leave your virtual environment, you need to issue the deactivate command from anywhere on the system:

$ deactivate 

Other:

For connecting Django to a Postgres db:

$ pip3 install psycopg2

Example of installing another module to use with Django, using alternate style:

$ python3 -m pip install requests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment