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 [07/12/2015 17:21] Hypathie [Avant de commencer] |
utilisateurs:hypathie:tutos:developpement-pyhon-utiliser-django [08/12/2015 14:41] 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 5: | Ligne 5: | ||
- | =====Avant de commencer ==== | + | ===== Installations ===== |
- | Sur Jessie python et python 3 sont tous deux installés. | + | **Installer les outils et bibliothèques pour développer en python ou python3** |
- | ==== Choisir sa version de Python ==== | + | <note tip> |
+ | Sur Jessie, python et python 3 sont tous deux installés. | ||
+ | </note> | ||
- | * Pour tout le système avec update-alternatives : | + | * Fichiers d'en-tête de Python |
- | <code root>update-alternatives --list python</code> | + | <code root>apt-get install python-dev python3-dev</code> |
- | <code retour>update-alternatives: error: no alternatives for python</code> | + | |
- | * Charger les différentes versions de Python : | + | * Pour l'algèbre avec scipy |
- | - __Pour voir où sont les versions de python disponibles :__ | + | <code root>apt-get install libblas3 libblas-dev liblapack3 liblapack-dev</code> |
- | <code user> ls /usr/bin/python*</code> | + | * Bibliothèques FreeType nécessaire par matplotlib |
- | <code retour> | + | |
- | /usr/bin/python /usr/bin/python3 /usr/bin/python3-config | + | |
- | /usr/bin/python2 /usr/bin/python3.4 /usr/bin/python3m | + | |
- | /usr/bin/python2.7 /usr/bin/python3.4-config /usr/bin/python3m-config | + | |
- | /usr/bin/python2.7-config /usr/bin/python3.4m /usr/bin/python-config | + | |
- | /usr/bin/python2-config /usr/bin/python3.4m-config | + | |
- | </code> | + | |
- | - __Pour ajouter à la liste de update-alternatives les deux versions de python disponibles :__\\ | + | <code=root>apt-get install libfreetype6 libfreetype6-dev</code> |
- | (ici par exemple; "/usr/bin/python2.7" et "/usr/bin/python3.4 ") | + | |
- | <code root>update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1</code> | + | * Dépendances nécessaire pour la cryptographie |
- | <code root>update-alternatives --install /usr/bin/python python /usr/bin/python3.4 2</code> | + | |
- | - __Pour voir la nouvelle liste de update-alternatives :__ | + | <code root>apt-get install libffi-dev libssl-dev</code> |
- | <code root>update-alternatives --list python</code> | + | * Les paquets Core Python |
- | <code retour> /usr/bin/python2.7 | + | |
- | /usr/bin/python3.4</code> | + | |
+ | <code root>apt-get install python-pip python3-pip</code> | ||
- | - __Pour basculer de l'une à l'autre version de Python :__ | ||
- | <code root>update-alternatives --config python</code> | + | <code root>apt-get install python-virtualenv</code> |
- | <code retour> | + | |
- | Il existe 2 choix pour l'alternative python (qui fournit /usr/bin/python). | + | |
- | Sélection Chemin Priorité État | + | <note> |
- | ------------------------------------------------------------ | + | À savoir sur Jessie, il est inutile de lancer : |
- | * 0 /usr/bin/python3.4 2 mode automatique | + | |
- | 1 /usr/bin/python2.7 1 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 | + | ''apt-get install python3-virtualenv'' \\ |
- | </code> | + | |
- | <code root>python --version</code> | + | En effet, avec la commande ''apt-get install python-virtualenv'' a installé ''python3-virtualenv'' et ''python-virtualenv'' |
- | <code retour>Python 2.7.9</code> | + | </note> |
- | * À savoir pour supprimer l'un des choix de la liste : ''update-alternatives --remove python /usr/bin/python2.7'' | + | <note> |
- | <note warning> | + | - ''python3-pip'' (ou ''python-pip'') : installateur de paquets python, Pip permet d'installer aisément des modules Python, à la manière dont on installe des paquets dans une distribution linux; |
- | Ne jamais désinstaller les versions inférieures à python 3 ! \\ | + | - ''python3-dev'' (ou ''python-dev'' ): permet d'utiliser des outils de développement pour la construction des modules Python, non pris en charge par pip, en particulier pour utiliser des bibliothèques avec des extensions en C |
- | De nombreux logiciels ne fonctionneraient plus. | + | - ''virtualenv'' : permet de créer un environnement cloisonné Python |
- | ''<del>apt-get purge python</del>'' | + | * À consulter : https://virtualenv.readthedocs.org/en/latest/ \\ https://github.com/neurite/debian-setup/wiki/Basic-Development-Setup-on-Debian |
</note> | </note> | ||
- | <note tip>**__Il est possible de laisser l'une des versions de python tout le système | + | * Installer la documentation python |
- | et d'utiliser l'autre pour l'environnement de l'utilisateur courant.__** | + | |
+ | <code root>apt-get install python-doc python3-doc</code> | ||
- | **Par exemple pour configurer Python3 pour l'utilisateur courant :** | + | * pour utiliser pyenv |
- | - __On édite le fichier "~/bashrc" :__ | + | <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> | ||
- | <code user>vim ~/.bashrc</code> | + | * Sans rapport avec le développement mais utilisé plus bas |
- | - __On ajoute cet alias :__ | + | <code root>apt-get install tree</code> |
- | <code> | ||
- | alias python='/usr/bin/python3.4' | ||
- | </code> | ||
- | - __On enregistre puis on actualise l'environnement de l'utilisateur courant :__ | + | ===== Créer des projets Django en python2 ou python3===== |
- | <code user>. ~/.bashrc</code> | + | ===Les versions de python sur sa Jessie === |
- | <code user>python --version</code> | + | <code user> ls /usr/bin/python*</code> |
+ | <code retour> | ||
+ | /usr/bin/python /usr/bin/python3 /usr/bin/python3-config | ||
+ | /usr/bin/python2 /usr/bin/python3.4 /usr/bin/python3m | ||
+ | /usr/bin/python2.7 /usr/bin/python3.4-config /usr/bin/python3m-config | ||
+ | /usr/bin/python2.7-config /usr/bin/python3.4m /usr/bin/python-config | ||
+ | /usr/bin/python2-config /usr/bin/python3.4m-config | ||
+ | </code> | ||
- | <code retour>Python 3.4.2</code> | + | <note tip> |
+ | L'idée suivie ici est de pouvoir aussi bien créer des projets Django codés en python 3 tandis que c'est la version python 2 qui est en place sur tout le système que l'inverse, c'est-à-dire, pouvoir créer des projects Django codés en python2 alors que c'est python 3 qui est mise en place sur tout le système. \\ | ||
- | </note> | + | Pour installer Django avec python3 y compris **dans un environnement virtuel on n'utilisera pas** ''virtualenv'' seul, afin de s'éviter quelque conflits lors de l'installation de django. \\ |
- | <note important> | + | En effet : |
- | **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. | + | <code user> |
- | Dans ce cas, on travaille dans un environnement virtuel avec l'outil ''virtualenv'', la suite du tuto suit ce choix.** | + | python --version |
+ | </code> | ||
+ | <code retour> | ||
+ | Python 3.4.2 | ||
+ | </code> | ||
- | </note> | + | <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 | ||
- | ==== Installer les outils et bibliothèques pour développer en python ou python 3 ==== | + | Successfully installed django |
+ | Cleaning up... | ||
+ | </code> | ||
- | * Fichiers d'en-tête de Python | + | :-/ |
- | <code=root>apt-get install python-dev python3-dev</code> | + | <code user>rm -r ~/MonPy3Django</code> |
- | * Pour l'algèbre avec scipy | + | </note> |
- | <code=root>apt-get install libblas3 libblas-dev liblapack3 liblapack-dev</code> | + | ==== Installer pyenv==== |
- | * Bibliothèques FreeType nécessaire par matplotlib | + | <code user>git clone https://github.com/yyuu/pyenv.git ~/.pyenv</code> |
+ | <code user>echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc</code> | ||
- | <code=root>apt-get install libfreetype6 libfreetype6-dev</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>] | ||
- | * Dépendances nécessaire pour la cryptographie | + | 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=root>apt-get install libffi-dev libssl-dev</code> | + | See `pyenv help <command>' for information on a specific command. |
- | + | For full documentation, see: https://github.com/yyuu/pyenv#readme</code> | |
- | * Les paquets Core Python | + | <code user>pyenv install 2.7.9</code> |
- | + | <code user>pyenv install 3.4.3</code> | |
- | <code=root>apt-get install python-pip python3-pip</code> | + | |
- | + | ||
- | + | ||
- | <code=root>apt-get install python-virtualenv python3-virtualenv</code> | + | |
<note> | <note> | ||
- | + | L'exécution des deux commandes précédentes est longue. Patience... | |
- | - ''python3-pip'' (ou ''python-pip'') : installateur de paquets python, Pip permet d'installer aisément des modules Python, à la manière dont on installe des paquets dans une distribution linux; | + | |
- | - ''python3-dev'' (ou ''python-dev'' ): permet d'utiliser des outils de développement pour la construction des modules Python, non pris en charge par pip, en particulier pour utiliser des bibliothèques avec des extensions en C | + | |
- | - ''virtualenv'' : permet de créer un environnement cloisonné Python | + | |
- | + | ||
- | * À consulter : https://virtualenv.readthedocs.org/en/latest/ \\ https://github.com/neurite/debian-setup/wiki/Basic-Development-Setup-on-Debian | + | |
</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 | ||
- | ===== Configurations et installation de Django avec SQLite3===== | + | <code user>pyenv +lacommande</code> |
+ | * Avoir une liste des commandes pyenv | ||
- | ==== Installer Django dans un environnement python virtuel ==== | + | <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> | ||
- | === On créer un répertoire par exemple sur son bureau :=== | + | * 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> | ||
- | <code user>cd ~/Bureau</code> | + | - Pour changer de version de python pour l'environnement virtuel, on fait la même chose avec la version 3 |
+ | <code user>pyenv shell 3.4.3</code> | ||
+ | <code user>python --version</code> | ||
+ | <code retour>Python 3.4.3</code> | ||
- | <code user>virtualenv PremiersProjetsDjango</code> | + | ====Utiliser virtualenv avec pyenv==== |
- | Cela a crée une dossier nommé "PremiersProjetsDjango" sur le bureau de l'utilisateur courant | + | * Configuration du système pour 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> | ||
- | ===on se place dans le dossier "PremiersProjetsDjango"=== | + | * Créer un répertoire où l'on pourra installer django pour python 3 |
- | <code user>cd PremiersProjetsDjango/</code> | + | <code user>mkdir ProjetsDjangoPy3 && cd ProjetsDjangoPy3</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>ls</code> | + | ====Installer Django ==== |
- | Il contient trois dossiers permettant d'utiliser python : | + | <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 | ||
- | <code retour>python python3 python3.4 </code> | + | Successfully installed django-1.9 |
+ | (1.8-3.4) hypathie@debian:~/MonPython3Django$ pip install django</code> | ||
+ | Pas d'erreur cette fois 8-) | ||
+ | * Pour vérifier les versions | ||
- | === On active l'environnement virtuel python=== | + | <code user>django-admin.py --version</code> |
+ | <code retour>1.9</code> | ||
- | <code user>source bin/activate</code> | + | <code user>python --version</code> |
+ | <code user>Python 3.4.3</code> | ||
- | <note tip> | + | Avec la commande recommandée par la doc officielle de django: |
- | **Pour sortir de l'environnement virtuel python :** | + | |
- | + | ||
- | <code user>deactivate</code> | + | |
- | </note> | + | |
- | + | ||
- | === On installe Django === | + | |
- | + | ||
- | <code user>pip3 install django</code> | + | |
- | + | ||
- | * Et pour vérifier tout en prenant connaissance de la version de django installée : | + | |
- | + | ||
- | (selon la méthode proposer par la doc officielle de Django : https://docs.djangoproject.com/fr/1.8/intro/tutorial01/) | + | |
<code user>python3 -c "import django; print(django.get_version())"</code> | <code user>python3 -c "import django; print(django.get_version())"</code> | ||
- | <code retour>1.9</code> | ||
- | <note tip> | + | aussi bien que : |
- | * **Tout est bien clair ? ** | + | |
- | <code user>deactivate</code> | + | <code user>python -c "import django; print(django.get_version())"</code> |
- | <code user>python3 -c "import django; print(django.get_version())"</code> | + | |
- | <code retour> | + | |
- | Traceback (most recent call last): | + | |
- | File "<string>", line 1, in <module> | + | |
- | ImportError: No module named 'django' | + | |
- | </code> | + | |
- | |||
- | <code user>source bin/activate</code> | ||
- | <code user>python3 -c "import django; print(django.get_version())"</code> | ||
<code retour>1.9</code> | <code retour>1.9</code> | ||
+ | 8-) | ||
- | * Pour avoir la liste de tous les modules python installés dans son environnement : | + | <note tip> |
- | + | * Pour sortir de pyenv : | |
- | <code user>pip freeze</code> | + | |
- | <code retour> | + | |
- | Django==1.9 | + | |
- | wheel==0.24.0 | + | |
- | </code> | + | |
- | * **Manuel officiel de pip** : https://pip.pypa.io/en/stable/user_guide/ | + | <code user>pyenv deactivate</code> |
</note> | </note> | ||
- | ==== Création d'un premier projet Django==== | + | ====Créer un projet django sous python 3==== |
<note> | <note> | ||
- | **Un petit rappel ?** | + | Si l'on reprend le wiki ici ! Ou qu'on a lancé la commande ''pyenv deactivate'' |
- | - On a crée un dossier par exemple sur son bureau pour l'environnement virtuel python "virtualenv PremiersProjetsDjango"; | + | * On va dans son répertoire configuré pour python 3 dans lequel est installé django |
- | - Dans ce dossier on a activé python "source bin/activate" ; | + | <code user>cd ~/ProjetsDjangoPy3/</code> |
- | - On a installé Django. | + | <code user>pyenv activate 1.8-3.4</code> |
- | + | ||
- | À partir de là pour créer un projet Django, il faut aller dans le dossier de l'environnement virtuel python et activer python : | + | |
- | + | ||
- | <code user>cd ~/Bureau/PremiersProjetsDjango/</code> | + | |
- | + | ||
- | <code user>source bin/activate</code> | + | |
- | + | ||
- | <code user>python3 -c "import django; print(django.get_version())"</code> | + | |
</note> | </note> | ||
- | * Le contenu de son dossier avant création d'un nouveau projet: | + | * Pour créer le projet Django nommé par exemple "premierProjet" |
- | + | ||
- | <code user>pwd</code> | + | |
- | <code retour>/home/hypathie/Bureau/PremiersProjetsDjango</code> | + | |
- | + | ||
- | <code user>ls</code> | + | |
- | <code retour>bin include lib</code> | + | |
- | + | ||
- | * Pour créer un nouveau projet nommé par exemple "premierProjet": | + | |
<code user>django-admin.py startproject premierProjet</code> | <code user>django-admin.py startproject premierProjet</code> | ||
- | <code user>ls</code> | ||
- | <code retour>bin include lib premierProjet</code> | ||
- | |||
- | * Architecture du dossier premierProjet : | ||
- | |||
- | <code user>cd premierProjet/</code> | ||
- | <code retour>manage.py premierProjet</code> | ||
- | |||
- | <code user>cd premierProjet/</code> | ||
- | <code retour>__init__.py settings.py urls.py wsgi.py</code> | ||
- | |||
- | * Avec la commande tree : | ||
<code user>tree premierProjet/</code> | <code user>tree premierProjet/</code> | ||
Ligne 319: | Ligne 340: | ||
{{http://pix.toile-libre.org/upload/img/1449147148.png}}\\ | {{http://pix.toile-libre.org/upload/img/1449147148.png}}\\ | ||
- | ====Premier projet avec la base de données sqlite3==== | + | |
+ | =====Premier projet avec la base de données sqlite3===== | ||
* Par défaut, Django utilise la base de donnée sqlite3 : | * Par défaut, Django utilise la base de donnée sqlite3 : |