0

Recebi um projeto para ser feito somente com o Python na v.3.4+ e não estou conseguindo colocar o Django a funcionar nas versões que não sejam 2.7. Alguém sabe um link que ensine toda a instalação do django nessa nova versão do Python?

3
  • Q sistema operacional?
    – vinibrsl
    21/06/2018 às 3:35
  • Windows 7 Professional 21/06/2018 às 4:09
  • Você não está conseguindo definir o Python 3.x para instalar o Django ou o Django foi instalado mas não funciona? 21/06/2018 às 10:47

1 Resposta 1

-2

Daniel no meu GitHub partilho os procedimentos para fazer esse setup.

Sugiro que utilizes ambientes virtuais para o desenvolvimento pois permite na mesma máquina, em ambientes diferentes, utilizar diferentes versões de Django.

Eu optei por utilizar Python 2. No entanto, se queres usar a versão 3 remove a 2 do Path (e não a 3) e usa o comando da versão de Python 3 para instalar os ambientes virtuais (vais perceber ao ver as instruções).

De seguida partilho o procedimento:

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

Podes encontrar tudo aqui: https://github.com/tiago-peres/Django/blob/master/procedures/Django_DevEnv_Admin.txt

3
  • 2
    Olá tiagoperes! Evite postar links pois eles podem ficar fora do ar, ser alterados, etc, além de facilitar a visualização e pesquisa SOpt. Utilize links para referenciar/complementar algo de sua resposta, ou um exemplo funcional (ex.: sqlifiddle). :)
    – rbz
    21/06/2018 às 10:13
  • 1
    tiagoperes muito obrigado, esses procedimentos foram de grande ajuda , consegui instalar o Py3.4(apesar de alguns erros fáceis de corrigir)a instalação ocorreu perfeitamente , exceto pelo passo 4 ao 5 que deu erro(creio que pela versão). Desde já obrigado. 21/06/2018 às 15:46
  • Grato por ter ajudado. Podes colocar as alterações necessárias no procedimento 21/06/2018 às 17:44

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .