Installing and Setting up a Django Project into a Virtual Environment

BlogInstalling and Setting up a Django Project into a Virtual Environment

Installing and Setting up a Django Project into a Virtual Environment

[trx_accordion counter=”on” initial=”1″]
[trx_accordion_item title=”Introduction”]The CodingEntrepreneurs have an excellent online Udemy course “Coding for Entrepreneurs: Learn Python, Django, and More.” Obviously to take the course you need to install Django. To that end, they have a great Youtube video.

Starting/stopping the video as I follow along is great, but I prefer to also have notes in outline format. Notes help me understand learning objectives and serve as a review after I’ve completed the lesson.

This post provides companion notes to their Youtube.

[/trx_accordion_item][trx_accordion_item title=”Install Python”]Udemy tutorial requires Python 2.7.5 or greater. From a terminal command prompt you can simply run python and check your installed version

If you need to upgrade your python version, goto python.org, download and install.[trx_block class=”codesample”]Oden:~ user$ python
Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type “help”, “copyright”, “credits” or “license” for more information.[/trx_block][trx_icon icon=”icon-info” color=”#1e73be”]When I ran the python installer, there was a comment about using pip2.7; which is not important as both pip and pip2.7 are linked to the same pip executible[/trx_accordion_item][trx_accordion_item title=”Verify Installation”]Verify the python upgrade, run python and verify its the correct version.[trx_block class=”codesample”]Oden:~ user$ python
Python 2.7.9 (v2.7.9:648dcafa7e5f, Dec 10 2014, 10:10:46)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type “help”, “copyright”, “credits” or “license” for more information.[/trx_block][/trx_accordion_item][trx_accordion_item title=”Install Package Installer for Python”][trx_icon icon=”icon-info” color=”#1e73be”] Its OK if version of PIP is slightly different (i.e. greater version) than what is listed here or in the youtube video.
Online documentation for pip is available.
[trx_block class=”codesample”]Oden:~ user$ sudo easy_install pip

WARNING: Improper use of the sudo command could lead to data loss
or the deletion of important system files. Please double-check your
typing when using sudo. Type “man sudo” for more information.

To proceed, enter your password, or type Ctrl-C to abort.

Password:
Searching for pip
Best match: pip 1.5.6
Adding pip 1.5.6 to easy-install.pth file
Installing pip script to /Library/Frameworks/Python.framework/Versions/2.7/bin
Installing pip3.4 script to /Library/Frameworks/Python.framework/Versions/2.7/bin
Installing pip3 script to /Library/Frameworks/Python.framework/Versions/2.7/bin

Using /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
Processing dependencies for pip
Finished processing dependencies for pip[/trx_block]
You can verify that a pip and pip2.7 are identical version.[trx_block class=”codesample”]Oden:~ user$ pip2.7 -V
pip 1.5.6 from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (python 2.7)

Oden:~ user$ pip -V
pip 1.5.6 from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (python 2.7)[/trx_block]
You can also verify that pip and pip2.7 are the identical executable file.[trx_block class=”codesample”]Oden:~ user$ find / -name pip2.7 2>/dev/null
/Library/Frameworks/Python.framework/Versions/2.7/bin/pip2.7
/usr/local/bin/pip2.7

Oden:~ user$ find / -name pip2>/dev/null
/Library/Frameworks/Python.framework/Versions/2.7/bin/pip
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip
/Users/user/Library/Enthought/Canopy_64bit/User/bin/pip
/Users/user/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pip-1.4.1-py2.7.egg/pip
/usr/local/bin/pip

Oden:~ user$ cd /usr/local/bin/
Oden:~ user$ ls pip*
lrwxrwxr-x 1 root admin 65 Dec 19 16:15 pip@ -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/pip
-rwxr-xr-x 1 root wheel 249 Sep 2 2013 pip-2.7*
lrwxrwxr-x 1 root admin 66 Dec 19 16:15 pip2@ -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/pip2
lrwxrwxr-x 1 root admin 68 Dec 19 16:15 pip2.7@ -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/pip2.7[/trx_block][/trx_accordion_item][trx_accordion_item title=”Install Python Virtual Environment”]Best practices separate Python projects into their own virtual environments, so that any changes to your main computers software doesn’t introduce issues. With Virtual environments your virtual environment can have its own Python version along with its own set of Python packages.
[trx_block class=”codesample”]Oden:~ user$ sudo pip install virtualenvl
Downloading/unpacking virtualenv
Downloading virtualenv-1.11.6-py2.py3-non-any.why (1.6MB): 1.6MB downloaded
Installing collected packages: virtualenv
Successfully installed virtualenv
Cleaning up…[/trx_block][/trx_accordion_item][trx_accordion_item title=”Create Virtual Environment for a Project”]You can create your project directory anywhere; below I’m showing it created on the users desktop.
[trx_icon icon=”icon-info” color=”#1e73be”]Note the tilde ~ character simply refers to the current user’s home directory.
[trx_icon icon=”icon-info” color=”#1e73be”]If at anytime you want to clear the terminal window use the ⌘-k shortcut.
[trx_block class=”codesample”]Oden:~ user$ cd ~/Desktop
Oden:~ user$ pwd
/Users/user/Desktop
Oden:~ user$ virtualenv LearnDjango

New python executable in LearnDjango/bin/python
Installing setuptools, pip…done.[/trx_block][/trx_accordion_item][trx_accordion_item title=”Activate the Project’s Virtual Environment”]Activating the project is required.
[trx_icon icon=”icon-info” color=”#1e73be”] bin/activate simply adds the VIRTUAL_ENV and changes the PATH, environmental variables.
[trx_icon icon=”icon-info” color=”#1e73be”] When the Virtual environment is active the command prompt is prefaced with the project name, in this case prefaced with (LearnDjango).[trx_block class=”codesample”]Oden:~ user$ cd LearnDjango
Oden:~ user$ pwd
/Users/user/Desktop/LearnDjango
Oden:~ user$ source bin/activate
(LearnDjango)Oden:LearnDjango user$
[/trx_block][/trx_accordion_item][trx_accordion_item title=”Verify Virtual Environment Installation”]To see and check what python packages are installed you can use the pip freeze or pip list commands.[trx_block class=”codesample”](LearnDjango)Oden:LearnDjango user$ pip freeze
wsgiref==0.1.2

(LearnDjango)Oden:LearnDjango user$ pip list
pip (1.5.6)
setuptools (3.6)
wsgiref (0.1.2)[/trx_block][/trx_accordion_item][trx_accordion_item title=”Install Django into Virtual Environment”]From the virtual environment directory, use pip.[trx_icon icon=”icon-info” color=”#1e73be”] If you are following the Udemy class, you can install the other python packages with “pip install django django-registration south stripe”[trx_block class=”codesample”](LearnDjango)Oden:LearnDjango user$ pip install django
Downloading/unpacking django
Downloading Django-1.7.1-py2.py3-none-any.whl (7.4MB): 7.4MB downloaded
Installing collected packages: django
Successfully installed django
Cleaning up…[/trx_block][/trx_accordion_item][trx_accordion_item title=”Verify Django Installation”]Rerun the pip freeze and see that Djano has been installed.[trx_block class=”codesample”](LearnDjango)Oden:LearnDjango user$ pip freeze
wsgiref==0.1.2
Django==1.7.1[/trx_block][/trx_accordion_item][trx_accordion_item title=”Create Django Project”]Creating a Django project uses the django-admin.py program.[trx_block class=”codesample”]Oden:~ user$ django-admin.py startproject my_project
[/trx_block][trx_icon icon=”icon-info” color=”#1e73be”]This is a good time to familiarize yourself with the standard project directory structure.[trx_block class=”codesample”]
Oden:~ user$ ls
total 8
drwxr-xr-x 7 user staff 238 Dec 20 15:02 ./
drwx——@ 21 user staff 714 Dec 20 14:42 ../
lrwxr-xr-x 1 user staff 56 Dec 20 14:42 .Python@ -> /Library/Frameworks/Python.framework/Versions/2.7/Python
drwxr-xr-x 17 user staff 578 Dec 20 14:55 bin/
drwxr-xr-x 3 user staff 102 Dec 20 14:42 include/
drwxr-xr-x 3 user staff 102 Dec 20 14:42 lib/
drwxr-xr-x 4 user staff 136 Dec 20 15:02 my_project/ [/trx_block][trx_icon icon=”icon-info” color=”#1e73be”]Best practice suggests renaming you project folder to src, via the finder or using “mv my_project src” from the terminal window command prompt.[/trx_accordion_item][trx_accordion_item title=”Start Django Server”]The manage.py is used to start the Django web server. For a complete list of commands available see the manage.py and django-admin.py documentation. [trx_block class=”codesample”](LearnDjango)Oden:LearnDjango user$ python manage.py runserver
Performing system checks…

System check identified no issues (0 silenced).

You have unapplied migrations; your app may not work properly until they are applied.
Run ‘python manage.py migrate’ to apply them.

December 20, 2014 – 23:05:16
Django version 1.7.1, using settings ‘my_project.settings’
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C[/trx_block][/trx_accordion_item][trx_accordion_item title=”View Django Project”]To test Django server and view the default application, open the server URL “http://127.0.0.1:8000/ in a web browser.”

DefaultDjangoServerStartPage[/trx_accordion_item][/trx_accordion]

Post a comment:

Comment

Type at least 1 character to search
Email Me:
philip@thesavvyfounder.com
Follow Me: