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 →
Ceci est une ancienne révision du document !
update-alternatives --list python
update-alternatives: error: no alternatives for python
- Pour voir où sont les version de python disponibles :
ls /usr/bin/python*
/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
- 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 ”)
update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
update-alternatives --install /usr/bin/python python /usr/bin/python3.4 2
- Pour voir la nouvelle liste de update-alternatives :
update-alternatives --list python
/usr/bin/python2.7 /usr/bin/python3.4
- Pour basculer de l'une à l'autre version de Python :
update-alternatives --config python
Il faudra lancer :
python3-pip python3-dev
et non :
python-pip python-dev
update-alternatives --config python
Il existe 2 choix pour l'alternative python (qui fournit /usr/bin/python). Sélection Chemin Priorité État ------------------------------------------------------------ * 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
python --version
Python 2.7.9
- On édite le fichier “~/bashrc” :
vim ~/.bashrc
- On ajoute cet alias :
alias python='/usr/bin/python3.4'
- On enregistre puis on actualise l'environnement de l'utilisateur courant :
. ~/.bashrc
python --version
Python 3.4.2
- On choisit d'activer Python3 que pour l'utilisateur courant et Python2 pour tout le système :
Ce choix est bon dans le cas où l'on veut s'exercer au code Python qui diffère entre les deux versions. On peut par exemple, se créer un utilisateur qui utilisera Python2
- On veut apprendre à coder en python directement avec le framework Django :
Il faut alors choisir une unique version de Python pour tout le système, car comme dit plus haut,\ les utiles python servant à utiliser Django s'installe en tant que root pour tous le système. Et l'installation pour tout le système des outils python et python3 génère des conflits.
Ou toujours travailler dans un environnement python virtuel en utilisant “ virtualenv” (voir plus bas).
update-alternatives --config python
Il existe 2 choix pour l'alternative python (qui fournit /usr/bin/python). Sélection Chemin Priorité État ------------------------------------------------------------ 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é :0
python --version
Python 3.4.2
update-alternatives --remove python /usr/bin/python2.7
apt-get install python3-pip python3-dev virtualenv
cd ~/Bureau
virtualenv PremiersProjetsDjango
Cela a crée une dossier nommé “PremiersProjetsDjango” sur le bureau de l'utilisateur courant
cd PremiersProjetsDjango/
ls
Il contient trois dossiers permettant d'utiliser python :
python python3 python3.4
source bin/activate
deactivate
pip3 install django
(selon la méthode proposer par la doc officielle de Django : https://docs.djangoproject.com/fr/1.8/intro/tutorial01/)
python3 -c "import django; print(django.get_version())"
1.9
deactivate
python3 -c "import django; print(django.get_version())"
Traceback (most recent call last): File "<string>", line 1, in <module> ImportError: No module named 'django'
source bin/activate
python3 -c "import django; print(django.get_version())"
1.9
pip freeze
Django==1.9 wheel==0.24.0
À partir de là pour créer un projet Django, il faut aller dans le dossier de l'environnement virtuel python et activer python :
cd ~/Bureau/PremiersProjetsDjango/
source bin/activate
python3 -c "import django; print(django.get_version())"
pwd
/home/hypathie/Bureau/PremiersProjetsDjango
ls
bin include lib
django-admin.py startproject premierProjet
ls
bin include lib premierProjet
cd premierProjet/
manage.py premierProjet
cd premierProjet/
__init__.py settings.py urls.py wsgi.py
tree premierProjet/
premierProjet/ ├── manage.py └── premierProjet ├── __init__.py ├── settings.py ├── urls.py └── wsgi.py
Ces fichiers sont :
Pour plus de détails voir la doc officielle de Django : https://docs.djangoproject.com/fr/1.8/intro/tutorial01/
cd ~/Bureau/PremiersProjetsDjango/premierProjet/
python3 manage.py runserver
Performing system checks... System check identified no issues (0 silenced). You have unapplied migrations; your app may not work properly until they are applied. Run 'python manage.py migrate' to apply them. December 03, 2015 - 12:43:53 Django version 1.9, using settings 'premierProjet.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
On obtient :
~/Bureau/PremiersProjetsDjango/premierProjet/premierProjet
vim settings.py
... DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } } ...
LANGUAGE_CODE = 'fr-fr' TIME_ZONE = 'Europe/Paris'
tree . ├── manage.py └── premierProjet ├── __init__.py ├── __pycache__ │ ├── __init__.cpython-34.pyc │ ├── settings.cpython-34.pyc │ ├── urls.cpython-34.pyc │ └── wsgi.cpython-34.pyc ├── settings.py ├── urls.py └── wsgi.py
python3 manage.py migrate
Operations to perform: Apply all migrations: admin, auth, contenttypes, sessions Running migrations: Rendering model states... DONE Applying contenttypes.0001_initial... OK Applying auth.0001_initial... OK Applying admin.0001_initial... OK Applying admin.0002_logentry_remove_auto_add... OK Applying contenttypes.0002_remove_content_type_name... OK Applying auth.0002_alter_permission_name_max_length... OK Applying auth.0003_alter_user_email_max_length... OK Applying auth.0004_alter_user_username_opts... OK Applying auth.0005_alter_user_last_login_null... OK Applying auth.0006_require_contenttypes_0002... OK Applying auth.0007_alter_validators_add_error_messages... OK Applying sessions.0001_initial... OK
tree . ├── db.sqlite3 ├── manage.py └── premierProjet ├── __init__.py ├── __pycache__ │ ├── __init__.cpython-34.pyc │ ├── settings.cpython-34.pyc │ ├── urls.cpython-34.pyc │ └── wsgi.cpython-34.pyc ├── settings.py ├── urls.py └── wsgi.py
La commande :
python3 manage.py makemigrations
No changes detected
permet de recharger les modifications.
cd ~/Bureau/PremiersProjetsDjango/premierProjet/
python3 manage.py createsuperuser
Username (leave blank to use 'hypathie'): Hypathie Email address: hypathie@gmx.fr Password: Password (again): Superuser created successfully.
python3 manage.py runserver 192.168.0.26:8000
Pour le savoir :
ifconfig
192.168.0.26:8000/admin