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 10:26] Hypathie [Avant de commencer] |
utilisateurs:hypathie:tutos:developpement-pyhon-utiliser-django [08/12/2015 13:37] Hypathie [Configurations et installation de Django avec SQLite3] |
||
---|---|---|---|
Ligne 54: | Ligne 54: | ||
<code root>apt-get install python-doc python3-doc</code> | <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 | * Sans rapport avec le développement mais utilisé plus bas | ||
Ligne 188: | Ligne 193: | ||
===== Configurations et installation de Django avec SQLite3===== | ===== Configurations et installation de Django avec SQLite3===== | ||
- | ==== Installer Django dans un environnement python virtuel ==== | + | ==== Installer pyenv==== |
- | === On créer un répertoire par exemple sur son bureau :=== | + | <code user>git clone https://github.com/yyuu/pyenv.git ~/.pyenv</code> |
+ | <code user>echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc</code> | ||
- | <code user>virtualenv MonPy3Django -p /usr/bin/python3.4</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>] | ||
- | Cela a crée une dossier nommé "PremiersProjetsDjango" dans le répertoire de l'utilisateur courant | + | 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> | ||
- | ===on se place dans le dossier "PremiersProjetsDjango"=== | + | <note> |
+ | L'exécution des deux commandes précédentes est longue. Patience... | ||
+ | </note> | ||
- | <code user>cd ~/MonPy3Django/ && ls</code> | + | <code user>pyenv versions</code> |
+ | <code retour>* system (set by /home/hypathie/.pyenv/version) | ||
+ | 2.7.9 | ||
+ | 3.4.3</code> | ||
+ | |||
+ | * les commandes pyenv | ||
- | Il contient trois dossiers permettant d'utiliser python : | + | <code user>pyenv +lacommande</code> |
- | <code retour>bin include lib</code> | + | * Avoir une liste des commandes pyenv |
- | Les outils sont dans bin/ | + | <code user>pyenv</code> |
- | + | <code retour>Some useful pyenv commands are: | |
- | <code user>tree bin/</code> | + | commands List all available pyenv commands |
- | <code retour> | + | local Set or show the local application-specific Python version |
- | bin/ | + | global Set or show the global Python version |
- | ├── activate | + | shell Set or show the shell-specific Python version |
- | ├── activate.csh | + | install Install a Python version using python-build |
- | ├── activate.fish | + | uninstall Uninstall a specific Python version |
- | ├── activate_this.py | + | rehash Rehash pyenv shims (run this after installing executables) |
- | ├── easy_install | + | version Show the current Python version and its origin |
- | ├── easy_install-3.4 | + | versions List all Python versions available to pyenv |
- | ├── pip | + | which Display the full path to an executable |
- | ├── pip3 | + | whence List all Python versions that contain the given executable |
- | ├── pip3.4 | + | |
- | ├── python -> python3.4 | + | |
- | ├── python3 -> python3.4 | + | |
- | └── python3.4 | + | |
</code> | </code> | ||
- | On voit que tout l'environnement pointe vers python3.4 | + | * 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 active l'environnement virtuel python=== | + | - 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> | ||
+ | ====Utiliser virtualenv avec pyenv==== | ||
+ | * Configuration de système pour que virtualenv s'utilise avec pyenv | ||
- | <note tip> | + | <code user>git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv</code> |
- | **Pour sortir de l'environnement virtuel python :** | + | <code user>echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc</code> |
+ | <code user>exec $SHELL</code> | ||
- | <code user>deactivate</code> | + | * Créer un répertoire pour django configuré en python 3 avec le shell pyenv |
- | </note> | + | |
- | === On installe 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>pip3 install django</code> | + | ====Installer Django ==== |
- | * Et pour vérifier tout en prenant connaissance de la version de django installée : | + | <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 | ||
- | (selon la méthode proposer par la doc officielle de Django : https://docs.djangoproject.com/fr/1.8/intro/tutorial01/) | + | Successfully installed django-1.9 |
- | + | (1.8-3.4) hypathie@debian:~/MonPython3Django$ pip install django</code> | |
- | <code user>python3 -c "import django; print(django.get_version())"</code> | + | Pas d'erreur cette fois 8-) |
+ | <code user>django-admin.py --version</code> | ||
<code retour>1.9</code> | <code retour>1.9</code> | ||
+ | <code user>pyenv deactivate</code> | ||
- | <note tip> | ||
- | * **Tout est bien clair ? ** | ||
- | <code user>deactivate</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 user>python3 -c "import django; print(django.get_version())"</code> | ||
<code retour>1.9</code> | <code retour>1.9</code> | ||
- | + | ====Créer un projet django ==== | |
- | * Pour avoir la liste de tous les modules python installés dans son environnement : | + | |
- | + | ||
- | <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/ | + | |
- | </note> | + | |
- | + | ||
- | + | ||
- | ==== Création d'un premier projet Django==== | + | |
- | + | ||
- | <note> | + | |
- | **Un petit rappel ?** | + | |
- | + | ||
- | - On a crée un dossier par exemple sur son bureau pour l'environnement virtuel python "virtualenv PremiersProjetsDjango"; | + | |
- | - Dans ce dossier on a activé python "source bin/activate" ; | + | |
- | - On a installé Django. | + | |
- | + | ||
- | À 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> | + | |
- | + | ||
- | * Le contenu de son dossier avant création d'un nouveau projet: | + | |
- | + | ||
- | <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> |