issue208:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| issue208:python [2024/08/31 16:52] – d52fr | issue208:python [2024/09/03 12:18] (Version actuelle) – auntiee | ||
|---|---|---|---|
| Ligne 4: | Ligne 4: | ||
| I have to make an apology to all of you. A few months back (FCM 204 and 205), I was discussing Sphinx and how to use it. When I explained the process of installing Sphinx, I showed how to create a virtual environment and how to activate, but I failed all of you by leaving it at that. Not only did I not show how to deactivate it, I failed to show how to reactivate it. I also failed all of you by not going deeper into virtual environments in general. In fact, I’ve never really discussed Virtual Environments before. I intend to fix those points now. We’ll be discussing all things Virtual this month.** | I have to make an apology to all of you. A few months back (FCM 204 and 205), I was discussing Sphinx and how to use it. When I explained the process of installing Sphinx, I showed how to create a virtual environment and how to activate, but I failed all of you by leaving it at that. Not only did I not show how to deactivate it, I failed to show how to reactivate it. I also failed all of you by not going deeper into virtual environments in general. In fact, I’ve never really discussed Virtual Environments before. I intend to fix those points now. We’ll be discussing all things Virtual this month.** | ||
| + | |||
| + | Virtuellement Python | ||
| + | |||
| + | Re-bonjour à tous les êtres vivants doués de sensibilité. Ici, sur la plateforme d' | ||
| + | |||
| + | Je dois m' | ||
| + | |||
| **What is a Virtual environment? | **What is a Virtual environment? | ||
| Ligne 12: | Ligne 19: | ||
| “This means it may not be possible for one Python installation to meet the requirements of every application. If application A needs version 1.0 of a particular module, but application B needs version 2.0, then the requirements are in conflict and installing either version 1.0 or 2.0 will leave one application unable to run.”** | “This means it may not be possible for one Python installation to meet the requirements of every application. If application A needs version 1.0 of a particular module, but application B needs version 2.0, then the requirements are in conflict and installing either version 1.0 or 2.0 will leave one application unable to run.”** | ||
| + | |||
| + | Qu' | ||
| + | |||
| + | Selon la documentation officielle de Python, un environnement virtuel est « une arborescence de répertoires autonome qui contient une installation de Python pour une version particulière de Python, ainsi qu'un certain nombre de paquets supplémentaires ». | ||
| + | |||
| + | En fait, lorsque vous utilisez un environnement virtuel, vous créez une « version » spéciale de Python qui n'a aucun paquet installé à l' | ||
| + | |||
| + | « Cela signifie qu'il peut être impossible pour une installation Python de répondre aux exigences de chaque application. Si l' | ||
| + | |||
| **I’ll be perfectly honest with you. I’m the world' | **I’ll be perfectly honest with you. I’m the world' | ||
| Ligne 20: | Ligne 36: | ||
| However, in the real world, you will eventually need some package that needs to be ‘installed’ via pip. Every time you install a third party package, you run the risk of having a version conflict. ** | However, in the real world, you will eventually need some package that needs to be ‘installed’ via pip. Every time you install a third party package, you run the risk of having a version conflict. ** | ||
| + | |||
| + | Je vais être parfaitement honnête avec vous. Je suis le PIRE au monde lorsqu' | ||
| + | |||
| + | Avez-vous VRAIMENT besoin d' | ||
| + | |||
| + | À proprement parler, non, vous n'avez pas BESOIN d' | ||
| + | |||
| + | Cependant, dans le monde réel, vous finirez par avoir besoin d'un paquet qui devra être « installé » via pip. Chaque fois que vous installez un paquet tiers, vous courez le risque d' | ||
| + | |||
| **So you can take the risk and just keep adding libraries to pip, or you can start using virtual environments. Just don’t say that I didn’t warn you. | **So you can take the risk and just keep adding libraries to pip, or you can start using virtual environments. Just don’t say that I didn’t warn you. | ||
| Ligne 32: | Ligne 57: | ||
| Python -m venv {path/ | Python -m venv {path/ | ||
| + | |||
| + | Vous pouvez donc prendre le risque d' | ||
| + | |||
| + | Comment gérer un environnement virtuel | ||
| + | |||
| + | Alors, comment commencer avec les environnements virtuels ? Vous devez créer le dossier de l' | ||
| + | |||
| + | Créer l' | ||
| + | |||
| + | La syntaxe pour créer un environnement virtuel est assez simple... | ||
| + | |||
| + | Python -m venv {chemin/ | ||
| + | |||
| **Normally, you will want to create the folder to hold the virtual environment named either venv or .venv in the project folder. You can also create a container directory that will hold many virtual environments like ~/ | **Normally, you will want to create the folder to hold the virtual environment named either venv or .venv in the project folder. You can also create a container directory that will hold many virtual environments like ~/ | ||
| Ligne 46: | Ligne 84: | ||
| (venv) greg@Earth2: | (venv) greg@Earth2: | ||
| + | |||
| + | Normalement, | ||
| + | |||
| + | En supposant que vous souhaitiez créer l' | ||
| + | |||
| + | $ python -m venv .venv | ||
| + | |||
| + | Une fois l' | ||
| + | |||
| + | $ source .venv/ | ||
| + | |||
| + | Vous devriez voir l' | ||
| + | |||
| + | (venv) greg@Earth2: | ||
| + | |||
| **As long as you are using the terminal to create commands, you will be using the virtual environments. However, if you do something outside of that terminal, you are using the default Python instance for your system and will not properly handle your project. | **As long as you are using the terminal to create commands, you will be using the virtual environments. However, if you do something outside of that terminal, you are using the default Python instance for your system and will not properly handle your project. | ||
| Ligne 58: | Ligne 111: | ||
| It’s always a good idea to deactivate the virtual environment before you close the terminal window. It should never break anything, but it’s better to tidy up behind yourself.** | It’s always a good idea to deactivate the virtual environment before you close the terminal window. It should never break anything, but it’s better to tidy up behind yourself.** | ||
| + | |||
| + | Tant que vous utilisez le terminal pour créer des commandes, vous utilisez les environnements virtuels. Cependant, si vous faites quelque chose en dehors de ce terminal-là, | ||
| + | |||
| + | Lorsque vous avez terminé, utilisez la commande deactivate pour libérer l' | ||
| + | |||
| + | Tapez simplement... | ||
| + | |||
| + | deactivate | ||
| + | |||
| + | dans le terminal et poursuivez votre chemin. | ||
| + | |||
| + | C'est toujours une bonne idée de désactiver l' | ||
| + | |||
| **Using Virtual environments with Pyenv | **Using Virtual environments with Pyenv | ||
| Ligne 80: | Ligne 146: | ||
| pyenv virtualenv 3.11.7 project1** | pyenv virtualenv 3.11.7 project1** | ||
| + | |||
| + | Utiliser les environnements virtuels avec Pyenv | ||
| + | |||
| + | Si vous utilisez pyenv pour gérer vos versions de Python, vous pouvez simplement utiliser les instructions ci-dessus, mais pyenv dispose d'une méthode spéciale pour gérer directement les environnements virtuels. | ||
| + | |||
| + | Lorsque vous avez installé pyenv, il devrait avoir installé l' | ||
| + | |||
| + | pyenv virtualenv {version de python} {nom du projet} | ||
| + | |||
| + | Disons que vous avez installé les versions suivantes de Python dans votre pyenv... | ||
| + | 3.8.10, | ||
| + | 3.9.10, | ||
| + | 3.10.10, | ||
| + | 3.10.12, | ||
| + | 3.11.4, | ||
| + | 3.11.7, | ||
| + | 3.12.0, | ||
| + | 3.12.2 | ||
| + | |||
| + | De plus, disons que vous voulez utiliser la version 3.11.7 pour ce projet et que vous voulez nommer l' | ||
| + | |||
| + | pyenv virtualenv 3.11.7 project1 | ||
| + | |||
| **Once you get the command-line prompt back, activate it by typing | **Once you get the command-line prompt back, activate it by typing | ||
| Ligne 94: | Ligne 183: | ||
| (project1) greg@Earth2: | (project1) greg@Earth2: | ||
| + | |||
| + | Une fois que vous avez retrouvé l' | ||
| + | |||
| + | pyenv activate project1 | ||
| + | |||
| + | L' | ||
| + | |||
| + | (projet1) greg@Earth2: | ||
| + | |||
| + | Cependant, contrairement à ce qui se passe avec Python simple, vous n' | ||
| + | |||
| + | Pour désactiver la version pyenv de l' | ||
| + | |||
| + | (projet1) greg@Earth2: | ||
| + | |||
| **Remember that when you create the pyenv virtual environment, | **Remember that when you create the pyenv virtual environment, | ||
| Well, that’s about it for this time. Until next time, as always; stay safe, healthy, positive and creative!** | Well, that’s about it for this time. Until next time, as always; stay safe, healthy, positive and creative!** | ||
| + | |||
| + | N' | ||
| + | |||
| + | Eh bien, c'est à peu près tout pour cette fois. Jusqu' | ||
issue208/python.1725115950.txt.gz · Dernière modification : 2024/08/31 16:52 de d52fr
