Vous n'êtes pas identifié(e).
L'icône rouge permet de télécharger chaque page du wiki visitée au format PDF et la grise au format ODT →
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente Prochaine révision Les deux révisions suivantes | ||
utilisateurs:hypathie:tutos:developpement-pyhon-utiliser-django [08/12/2015 06:47] Hypathie [Avant de commencer] |
utilisateurs:hypathie:tutos:developpement-pyhon-utiliser-django [08/12/2015 13:27] Hypathie [Configurations et installation de Django avec SQLite3] |
||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | ====== Développer en Python avec Django====== | + | ====== Développer en Python3 avec Django====== |
* Objet : Installer un environnement virtuel avec python3 pour utiliser Django \\ essayer un mini projet avec chacune des différentes bases de données supportées par ORM de Django. | * Objet : Installer un environnement virtuel avec python3 pour utiliser Django \\ essayer un mini projet avec chacune des différentes bases de données supportées par ORM de Django. | ||
Ligne 13: | Ligne 13: | ||
* Fichiers d'en-tête de Python | * Fichiers d'en-tête de Python | ||
- | <code=root>apt-get install python-dev python3-dev</code> | + | <code root>apt-get install python-dev python3-dev</code> |
* Pour l'algèbre avec scipy | * Pour l'algèbre avec scipy | ||
- | <code=root>apt-get install libblas3 libblas-dev liblapack3 liblapack-dev</code> | + | <code root>apt-get install libblas3 libblas-dev liblapack3 liblapack-dev</code> |
* Bibliothèques FreeType nécessaire par matplotlib | * Bibliothèques FreeType nécessaire par matplotlib | ||
Ligne 25: | Ligne 25: | ||
* Dépendances nécessaire pour la cryptographie | * Dépendances nécessaire pour la cryptographie | ||
- | <code=root>apt-get install libffi-dev libssl-dev</code> | + | <code root>apt-get install libffi-dev libssl-dev</code> |
* Les paquets Core Python | * Les paquets Core Python | ||
- | <code=root>apt-get install python-pip python3-pip</code> | + | <code root>apt-get install python-pip python3-pip</code> |
- | <code=root>apt-get install python-virtualenv python3-virtualenv</code> | + | <code root>apt-get install python-virtualenv</code> |
+ | |||
+ | <note> | ||
+ | À savoir sur Jessie, il est inutile de lancer : | ||
+ | |||
+ | ''apt-get install python3-virtualenv'' \\ | ||
+ | |||
+ | En effet, avec la commande ''apt-get install python-virtualenv'' a installé ''python3-virtualenv'' et ''python-virtualenv'' | ||
+ | </note> | ||
<note> | <note> | ||
Ligne 43: | Ligne 51: | ||
</note> | </note> | ||
+ | * Installer la documentation python | ||
+ | <code root>apt-get install python-doc python3-doc</code> | ||
+ | |||
+ | * pour utiliser pyenv | ||
+ | |||
+ | <code root>apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev \ | ||
+ | libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev</code> | ||
+ | |||
+ | * Sans rapport avec le développement mais utilisé plus bas | ||
+ | |||
+ | <code root>apt-get install tree</code> | ||
====Préparer son système pour travailler avec python2 et python3==== | ====Préparer son système pour travailler avec python2 et python3==== | ||
- | * Utiliser update-alternatives pour choisir l'une ou l'autre des versions: | + | ===Utiliser update-alternatives pour choisir l'une ou l'autre des versions:=== |
+ | |||
+ | * Charger les différentes versions de Python : | ||
<code root>update-alternatives --list python</code> | <code root>update-alternatives --list python</code> | ||
<code retour>update-alternatives: error: no alternatives for python</code> | <code retour>update-alternatives: error: no alternatives for python</code> | ||
- | * Charger les différentes versions de Python : | + | * Pour voir où sont les exécutables des deux versions disponibles de python : |
- | + | ||
- | - __Pour voir où sont les versions de python disponibles :__ | + | |
<code user> ls /usr/bin/python*</code> | <code user> ls /usr/bin/python*</code> | ||
Ligne 65: | Ligne 84: | ||
</code> | </code> | ||
- | - __Pour ajouter à la liste de update-alternatives les deux versions de python disponibles :__\\ | + | * Pour ajouter à la liste de update-alternatives les deux versions de python disponibles : |
(ici par exemple; "/usr/bin/python2.7" et "/usr/bin/python3.4 ") | (ici par exemple; "/usr/bin/python2.7" et "/usr/bin/python3.4 ") | ||
Ligne 71: | Ligne 90: | ||
<code root>update-alternatives --install /usr/bin/python python /usr/bin/python3.4 2</code> | <code root>update-alternatives --install /usr/bin/python python /usr/bin/python3.4 2</code> | ||
- | - __Pour voir la nouvelle liste de update-alternatives :__ | + | - Pour voir la nouvelle liste de update-alternatives : |
<code root>update-alternatives --list python</code> | <code root>update-alternatives --list python</code> | ||
Ligne 78: | Ligne 97: | ||
- | - __Pour basculer de l'une à l'autre version de Python :__ | + | - On va laisser tout le système configurer avec python3 dans un premier temps : |
<code root>update-alternatives --config python</code> | <code root>update-alternatives --config python</code> | ||
Ligne 90: | Ligne 109: | ||
2 /usr/bin/python3.4 2 mode manuel | 2 /usr/bin/python3.4 2 mode manuel | ||
- | Appuyez sur <Entrée> pour conserver la valeur par défaut[*] ou choisissez le numéro sélectionné :1 | + | Appuyez sur <Entrée> pour conserver la valeur par défaut[*] ou choisissez le num |
</code> | </code> | ||
+ | |||
+ | On choisit la sélection correspondante à python2.7 pour tout le système. Ici c'est le choix n°1. | ||
+ | |||
+ | <note warning> | ||
+ | * À savoir pour supprimer l'un des choix de la liste : | ||
+ | |||
+ | <code root>update-alternatives --remove python /usr/bin/python2.7</code> | ||
+ | |||
+ | * Ne jamais désinstaller les versions inférieures à python 3 ! \\ De nombreux logiciels ne fonctionneraient plus. | ||
+ | |||
+ | ''<del>apt-get purge python</del>'' | ||
+ | </note> | ||
+ | |||
+ | * Pour vérifier | ||
<code root>python --version</code> | <code root>python --version</code> | ||
<code retour>Python 2.7.9</code> | <code retour>Python 2.7.9</code> | ||
- | * À savoir pour supprimer l'un des choix de la liste : ''update-alternatives --remove python /usr/bin/python2.7'' | + | <note tip> |
+ | L'idée suivie ici sera de conserver python2 pour tout le système, et de mettre en place python3 dans un environnement virtuel consacré aux projets django. \\ | ||
- | <note warning> | + | Pour installer Django avec python3 y compris **dans un environnement virtuel on n'utilisera pas** ''virtualenv'', afin de s'éviter quelque conflits lors de l'installation de django. \\ |
- | Ne jamais désinstaller les versions inférieures à python 3 ! \\ | + | |
- | De nombreux logiciels ne fonctionneraient plus. | + | En effet : |
+ | |||
+ | <code user> | ||
+ | python --version | ||
+ | </code> | ||
+ | <code retour> | ||
+ | Python 3.4.2 | ||
+ | </code> | ||
+ | |||
+ | <code user>virtualenv MonPy3Django -p /usr/bin/python3.4</code> | ||
+ | <code user>cd MonPy3Django/</code> | ||
+ | <code user>source bin/activate 1.8-3.4</code> | ||
+ | <code retour>Downloading/unpacking django | ||
+ | Downloading Django-1.9-py2.py3-none-any.whl (6.6MB): 6.6MB downloaded | ||
+ | Installing collected packages: django | ||
+ | *** Error compiling '/tmp/pip-build-c20d1nfa/django/django/conf/app_template/apps.py'... | ||
+ | File "/tmp/pip-build-c20d1nfa/django/django/conf/app_template/apps.py", line 4 | ||
+ | class {{ camel_case_app_name }}Config(AppConfig): | ||
+ | ^ | ||
+ | SyntaxError: invalid syntax | ||
+ | |||
+ | *** Error compiling '/tmp/pip-build-c20d1nfa/django/django/conf/app_template/models.py'... | ||
+ | File "/tmp/pip-build-c20d1nfa/django/django/conf/app_template/models.py", line 1 | ||
+ | {{ unicode_literals }}from django.db import models | ||
+ | ^ | ||
+ | SyntaxError: invalid syntax | ||
+ | |||
+ | Successfully installed django | ||
+ | Cleaning up... | ||
+ | </code> | ||
- | ''<del>apt-get purge python</del>'' | ||
</note> | </note> | ||
- | <note tip>**__Il est possible de laisser l'une des versions de python tout le système | ||
- | et d'utiliser l'autre pour l'environnement de l'utilisateur courant.__** | ||
- | **Par exemple pour configurer Python3 pour l'utilisateur courant :** | ||
- | - __On édite le fichier "~/bashrc" :__ | + | ===Configurer son système avec Python3 pour l'utilisateur courant :=== |
+ | |||
+ | * On édite le fichier "~/bashrc" : | ||
<code user>vim ~/.bashrc</code> | <code user>vim ~/.bashrc</code> | ||
- | - __On ajoute cet alias :__ | + | * On ajoute cet alias : |
<code> | <code> | ||
Ligne 121: | Ligne 182: | ||
</code> | </code> | ||
- | - __On enregistre puis on actualise l'environnement de l'utilisateur courant :__ | + | * On enregistre puis on actualise l'environnement de l'utilisateur courant : |
<code user>. ~/.bashrc</code> | <code user>. ~/.bashrc</code> | ||
Ligne 129: | Ligne 190: | ||
<code retour>Python 3.4.2</code> | <code retour>Python 3.4.2</code> | ||
- | </note> | ||
- | <note tip> | + | ===== Configurations et installation de Django avec SQLite3===== |
- | **Une bonne solution pour débuter est de ne pas choisir une des versions de python pour tout le système ou l'environnement de l'utilisateur courant. | + | ==== Installer Django dans un environnement virtuel avec pyenv==== |
- | Dans ce cas, on travaille dans un environnement virtuel avec l'outil ''virtualenv'', la suite du tuto suit ce choix.** | + | <code user>git clone https://github.com/yyuu/pyenv.git ~/.pyenv</code> |
+ | <code user>echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc</code> | ||
+ | <code user>echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc</code> | ||
+ | <code user>echo 'eval "$(pyenv init -)"' >> ~/.bashrc</code> | ||
+ | <code user>exec $SHELL</code> | ||
+ | <code user>pyenv</code> | ||
+ | <code retour>pyenv 20151124-16-g132179d | ||
+ | Usage: pyenv <command> [<args>] | ||
+ | |||
+ | Some useful pyenv commands are: | ||
+ | commands List all available pyenv commands | ||
+ | local Set or show the local application-specific Python version | ||
+ | global Set or show the global Python version | ||
+ | shell Set or show the shell-specific Python version | ||
+ | install Install a Python version using python-build | ||
+ | uninstall Uninstall a specific Python version | ||
+ | rehash Rehash pyenv shims (run this after installing executables) | ||
+ | version Show the current Python version and its origin | ||
+ | versions List all Python versions available to pyenv | ||
+ | which Display the full path to an executable | ||
+ | whence List all Python versions that contain the given executable | ||
+ | |||
+ | See `pyenv help <command>' for information on a specific command. | ||
+ | For full documentation, see: https://github.com/yyuu/pyenv#readme</code> | ||
+ | <code user>pyenv install 2.7.9</code> | ||
+ | <code user>pyenv install 3.4.3</code> | ||
+ | |||
+ | <note> | ||
+ | L'exécution des deux commandes précédentes est longue. Patience... | ||
</note> | </note> | ||
+ | <code user>pyenv versions</code> | ||
+ | <code retour>* system (set by /home/hypathie/.pyenv/version) | ||
+ | 2.7.9 | ||
+ | 3.4.3</code> | ||
+ | | ||
+ | * les commandes pyenv | ||
+ | <code user>pyenv +lacommande</code> | ||
- | ===== Configurations et installation de Django avec SQLite3===== | + | * Avoir une liste des commandes pyenv |
+ | <code user>pyenv</code> | ||
+ | <code retour>Some useful pyenv commands are: | ||
+ | commands List all available pyenv commands | ||
+ | local Set or show the local application-specific Python version | ||
+ | global Set or show the global Python version | ||
+ | shell Set or show the shell-specific Python version | ||
+ | install Install a Python version using python-build | ||
+ | uninstall Uninstall a specific Python version | ||
+ | rehash Rehash pyenv shims (run this after installing executables) | ||
+ | version Show the current Python version and its origin | ||
+ | versions List all Python versions available to pyenv | ||
+ | which Display the full path to an executable | ||
+ | whence List all Python versions that contain the given executable | ||
+ | </code> | ||
- | ==== Installer Django dans un environnement python virtuel ==== | + | * Configurer la version du shell pyenv pour choisir sa version de python |
+ | - Pour que le shell utilise la version 2 installée plutôt | ||
+ | <code user>pyenv shell 2.7.9</code> | ||
+ | - Pour savoir quelle est la version de son shell | ||
+ | <code user>pyenv shell</code> | ||
+ | <code retour>2.7.9</code> | ||
+ | - version de python utilisée | ||
+ | <code user>python --version</code> | ||
+ | <code retour>Python 2.7.9</code> | ||
+ | - Depuis où ? | ||
+ | <code user> which python</code> | ||
+ | <code user>/home/hypathie/.pyenv/shims/python</code> | ||
- | === On créer un répertoire par exemple sur son bureau :=== | + | - Pour changer de version de python pour l'environnement virtuel, on fait la même chose avec la version |
+ | <code user>pyenv shell 3.4.3</code> | ||
+ | <code user>python --version</code> | ||
+ | <code retour>Python 3.4.3</code> | ||
- | <code user>cd ~/Bureau</code> | + | * Utiliser virtualenv avec pyenv |
+ | <code user>git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv</code> | ||
+ | <code user>echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc</code> | ||
+ | <code user>exec $SHELL</code> | ||
- | <code user>virtualenv PremiersProjetsDjango</code> | + | * Créer un répertoire pour django |
+ | <code user>mkdir MonPython3Django</code> | ||
+ | <code user>cd MonPython3Django/</code> | ||
+ | <code user>pyenv shell 3.4.3</code> | ||
+ | <code user>python --version</code> | ||
+ | <code retour>Python 3.4.3</code> | ||
+ | <code user>pyenv virtualenv 1.8-3.4</code> | ||
+ | <code retour>Ignoring indexes: https://pypi.python.org/simple | ||
+ | Requirement already satisfied (use --upgrade to upgrade): setuptools in /home/hypathie/.pyenv/versions/3.4.3/envs/1.8-3.4/lib/python3.4/site-packages | ||
+ | Requirement already satisfied (use --upgrade to upgrade): pip in /home/hypathie/.pyenv/versions/3.4.3/envs/1.8-3.4/lib/python3.4/site-packages | ||
+ | </code> | ||
+ | <code user>pyenv activate 1.8-3.4</code> | ||
+ | <code user>pip install django</code> | ||
+ | <code retour>You are using pip version 6.0.8, however version 7.1.2 is available. | ||
+ | You should consider upgrading via the 'pip install --upgrade pip' command. | ||
+ | Collecting django | ||
+ | Downloading Django-1.9-py2.py3-none-any.whl (6.6MB) | ||
+ | 100% |################################| 6.6MB 88kB/s | ||
+ | Installing collected packages: django | ||
- | Cela a crée une dossier nommé "PremiersProjetsDjango" sur le bureau de l'utilisateur courant | + | Successfully installed django-1.9 |
+ | (1.8-3.4) hypathie@debian:~/MonPython3Django$ pip install django</code> | ||
+ | Pas d'erreur cette fois 8-) | ||
+ | <code user>django-admin.py --version</code> | ||
+ | <code retour>1.9</code> | ||
+ | <code user>pyenv deactivate</code> | ||
- | ===on se place dans le dossier "PremiersProjetsDjango"=== | + | <code user>python3 -c "import django; print(django.get_version())"</code> |
+ | <code retour>1.9</code> | ||
- | <code user>cd PremiersProjetsDjango/</code> | + | ===Créer un projet django === |
- | <code user>ls</code> | + | <code user>django-admin.py startproject premierProjet</code> |
+ | <code user>cd premierProjet/</code> | ||
+ | <code user>tree</code> | ||
+ | <code retour> | ||
+ | . | ||
+ | ├── manage.py | ||
+ | └── premierProjet | ||
+ | ├── __init__.py | ||
+ | ├── settings.py | ||
+ | ├── urls.py | ||
+ | └── wsgi.py</code> | ||
+ | <code user> | ||
+ | |||
+ | </code> | ||
+ | |||
+ | |||
+ | ==== Installer Django dans un environnement python virtuel ==== | ||
+ | |||
+ | === On créer un répertoire=== | ||
+ | |||
+ | <code user>virtualenv MonPy3Django -p /usr/bin/python3.4</code> | ||
+ | |||
+ | Cela a crée une dossier nommé "PremiersProjetsDjango" dans le répertoire de l'utilisateur courant | ||
+ | |||
+ | |||
+ | ===on se place dans le dossier "PremiersProjetsDjango"=== | ||
+ | |||
+ | <code user>cd ~/MonPy3Django/ && ls</code> | ||
Il contient trois dossiers permettant d'utiliser python : | Il contient trois dossiers permettant d'utiliser python : | ||
- | <code retour>python python3 python3.4 </code> | + | <code retour>bin include lib</code> |
+ | Les outils sont dans bin/ | ||
+ | |||
+ | <code user>tree bin/</code> | ||
+ | <code retour> | ||
+ | bin/ | ||
+ | ├── activate | ||
+ | ├── activate.csh | ||
+ | ├── activate.fish | ||
+ | ├── activate_this.py | ||
+ | ├── easy_install | ||
+ | ├── easy_install-3.4 | ||
+ | ├── pip | ||
+ | ├── pip3 | ||
+ | ├── pip3.4 | ||
+ | ├── python -> python3.4 | ||
+ | ├── python3 -> python3.4 | ||
+ | └── python3.4 | ||
+ | </code> | ||
+ | |||
+ | On voit que tout l'environnement pointe vers python3.4 | ||
=== On active l'environnement virtuel python=== | === On active l'environnement virtuel python=== | ||
- | <code user>source bin/activate</code> | + | |
<note tip> | <note tip> |