Installing Django with Python3

Asked

Viewed 345 times

0

I received a project to be done only with Python in v.3.4+ and I’m not getting Django to work in versions other than 2.7. Does anyone know a link that teaches the entire installation of Django in this new version of Python?

  • Q operating system?

  • Windows 7 Professional

  • You are unable to set Python 3.x to install Django or Django has been installed but it does not work?

1 answer

-2


Daniel on my Github I share the procedures for doing this setup.

I suggest that you use virtual environments for development because it allows you to use different versions of Django on the same machine in different environments.

I chose to use Python 2. However, if you want to use version 3 remove version 2 from Path (not version 3) and use the Python version 3 command to install virtual environments (you’ll notice when you see the instructions).

Then I share the procedure:

Python, Django, and MySQL///////////////////////////////////////////////////////////////////////////////////////////////////////////////

/******

Experienced Python/Django developers often choose to instead run their Python apps within independent Python virtual environments.
These allow developers to have multiple different Django environments on a single computer, allowing them to create new websites (using the latest version of Django) 
while still maintaining websites that rely on older versions.
The Django developer team itself recommends that you use Python virtual environments.

That's how this tutorial is going to be about!

******/
0. Access PowerShell as admin
1. Create the main folder in the root:

PS > cd C:\
PS > mkdir PythonProjects
PS > cd PythonProjects

2. a) Get Python 2.7 (32 bits is the one I suggest) - install in your path - (https://www.python.org/downloads/release/python-2712/)

Customize the location: C:\Python27

Once you’ve installed Python,
open up a PowerShell window and type python and press enter
PS C:\PythonProjects>  python

This is what you will see:
-------------------

PS C:\PythonProjects> python
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:19:22) [MSC v.150
Type "help", "copyright", "credits" or "license" for more information.
>>>
------------------- (CTRL + Z to exit the Python prompt)

b) Get Python 3.5.2 (32 bits is the one I suggest) - install off your path - https://www.python.org/downloads/release/python-352/

Customize the location: C:\Python35-32

3. Get Pip

Save the following script as get-pip.py:
--------------
https://bootstrap.pypa.io/get-pip.py  (if you can not find it, ask me for a copy)
--------------

PS C:\PythonProjects> python get-pip.py

To check if everything is working, just type pip at the command line:

PS C:\PythonProjects> pip
PS C:\PythonProjects> pip install --upgrade setuptools
PS C:\PythonProjects> pip install ez_setup


4. Install virtualenv and virtualenvwrapper-powershell

PS C:\PythonProjects> pip install virtualenv
PS C:\PythonProjects> pip install virtualenvwrapper-powershell

5. Create, activate and deactivate virtual environment
PS C:\PythonProjects\virtualenvs> virtualenv rrh 
PS C:\PythonProjects\virtualenvs> Set-ExecutionPolicy Unrestricted -Force
PS C:\PythonProjects\virtualenvs> rrh\Scripts\activate
(rrh) PS C:\PythonProjects\virtualenvs> deactivate    // to put things back to normal.

Note: It's using Python version 2.7. If we want it to use the Python 3.5.2, just run the following command while creating the virtualenv:
PS C:\PythonProjects\virtualenvs> virtualenv --python=c:\Python35-32\python.exe envname

7. Install Django
(rrh) PS C:\PythonProjects\virtualenvs\rrh> pip install django

Start an interactive interpreter by typing
(rrh) PS C:\PythonProjects\virtualenvs\rrh> python
-----------------------------------
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:19:22) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
-----------------------------------

Great. As you can see, version 2.7.12 is installed in this virtualenv -> the version used globally in our machine.
It's great because MySQL-Python is not yet supported in Python-3.0.

(rrh) PS C:\PythonProjects\virtualenvs> cd ..
(rrh) PS C:\PythonProjects\> mkdir rrh
(rrh) PS C:\PythonProjects\> cd rrh 
(rrh) PS C:\PythonProjects\rrh> python -m django-admin startproject webapp // we are calling to our project name 'webapp'.

You should now see the project’s folder in our Django directory (C:\PythonProjects\rrh

(rrh) PS C:\PythonProjects\rrh> cd webapp
(rrh) PS C:\PythonProjects\rrh\webapp> dir

(rrh) PS C:\PythonProjects\rrh\webapp> python manage.py //Manage.py is Django’s command line utility; you should see a list of its available subcommands.
(rrh) PS C:\PythonProjects\rrh\webapp> python manage.py runserver //start up Django’s development server.

8. Setup MySQL and Install MySQL-python in your virtualenv

For the MySQL,
Download:
https://dev.mysql.com/downloads/mysql/   (Check Include Bin Directory in Windows PATH box.)

When prompted, set a password for the MySQL root account.
Create database, create a user and grant the user database access.

For the MySQL-Python,
Download: https://pypi.python.org/pypi/MySQL-python/1.2.5

After downloading, do not run the Windows installer.
(rrh) PS C:\PythonProjects\rrh\webapp> cd /PythonProjects
(rrh) PS C:\PythonProjects> mkdir downloads

(rrh) PS C:\PythonProjects> mv  -v ~/Downloads/MySQL-python-1.2.5.win32-py2.7.exe /PythonProjects/downloads

(rrh) PS C:\PythonProjects\rrh\webapp> easy_install file://c:/PythonProjects/downloads/MySQL-python-1.2.5.win32-py2.7.exe

(rrh) PS C:\PythonProjects\rrh\webapp> deactivate

9. Setting Django up to use MySQL (https://docs.djangoproject.com/en/dev/ref/databases/#mysql-notes)

open settings.py,
and update the default key in the DATABASES dictionary.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'DB_NAME',
        'USER': 'DB_USER',
        'PASSWORD': 'DB_PASSWORD',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    }
}

PS C:\PythonProjects\rrh\webapp> cd ..
PS C:\PythonProjects\rrh> cd ..
PS C:\PythonProjects> virtualenvs\rrh\Scripts\activate
(rrh) PS C:\PythonProjects> cd rrh/webapp //root of our Django project.
(rrh) PS C:\PythonProjects\rrh\webapp> python manage.py migrate //

You have Python, Django, and MySQL communicating in harmony.
Congratulations!
Let's deactivate our virtualenv and close PowerShell 

(rrh) PS C:\PythonProjects\rrh\webapp> deactivate
PS C:\PythonProjects\rrh\webapp> exit

You can find everything here: https://github.com/tiago-peres/Django/blob/master/procedures/Django_DevEnv_Admin.txt

  • 2

    Hello tiagoperes! Avoid posting links as they may get out of the air, be changed, etc., plus facilitate viewing and searching Sopt. Use links to reference/complement something of your response, or a functional example (e.g., sqlifiddle). :)

  • 1

    tiagoperes thank you very much, these procedures were of great help , I was able to install the Py3.4 (despite some errors easy to fix)the installation went perfectly , except for the step 4 to the 5 that gave error(I believe by the version). Thanks in advance.

  • Thanks for helping. You can put the necessary changes in the procedure

Browser other questions tagged

You are not signed in. Login or sign up in order to post.