issue208:python
                Différences
Ci-dessous, les différences entre deux révisions de la page.
| Prochaine révision | Révision précédente | ||
| issue208:python [2024/08/31 14:26] – créée auntiee | issue208:python [2024/09/03 12:18] (Version actuelle) – auntiee | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| - | + | **Virtually Python | |
| - | Virtually Python | + | |
| Greetings again fellow Sentient Lifeforms. Things here at landing pad 2997 on Terra still haven’t calmed down yet. Hopefully, by the end of the month, I’ll see things starting to calm down as we begin to transition to Autumn. | Greetings again fellow Sentient Lifeforms. Things here at landing pad 2997 on Terra still haven’t calmed down yet. Hopefully, by the end of the month, I’ll see things starting to calm down as we begin to transition to Autumn. | ||
| - | 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? | 
| According to the Python official documentation, | According to the Python official documentation, | ||
| Ligne 12: | Ligne 18: | ||
| Basically, when you use a Virtual environment, | Basically, when you use a Virtual environment, | ||
| - | “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.”** | 
| - | I’ll be perfectly honest with you. I’m the world' | + | 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' | ||
| Do you REALLY need to use a Virtual environment? | Do you REALLY need to use a Virtual environment? | ||
| Ligne 20: | Ligne 35: | ||
| Strictly speaking, no, you don’t NEED to use a Virtual environment. If you never intend to use any third-party libraries, and the basic Python “built in” libraries are all you will ever need, then you don’t need to worry about Virtual environments. | Strictly speaking, no, you don’t NEED to use a Virtual environment. If you never intend to use any third-party libraries, and the basic Python “built in” libraries are all you will ever need, then you don’t need to worry about Virtual environments. | ||
| - | 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. | 
| Dealing with a Virtual environment | Dealing with a Virtual environment | ||
| Ligne 32: | Ligne 56: | ||
| The syntax to create a Virtual environment is pretty simple… | The syntax to create a Virtual environment is pretty simple… | ||
| - | Python -m venv {path/ | + | Python -m venv {path/ | 
| - | 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 ~/ | + | 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 ~/ | ||
| Assuming you want to create the Virtual environment in your project folder (which I suggest is the best way to do things), then, in a terminal window from your project folder, type… | Assuming you want to create the Virtual environment in your project folder (which I suggest is the best way to do things), then, in a terminal window from your project folder, type… | ||
| Ligne 45: | Ligne 82: | ||
| You should see your terminal prompt change to remind you that you are using a virtual environment. | You should see your terminal prompt change to remind you that you are using a virtual environment. | ||
| + | |||
| + | (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: | (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. | ||
| When you are done, use the deactivate command to release the virtual environment. | When you are done, use the deactivate command to release the virtual environment. | ||
| Ligne 58: | Ligne 110: | ||
| in the terminal and go on your merry way. | in the terminal and go on your merry way. | ||
| - | 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.** | 
| - | Using Virtual environments with Pyenv | + | 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 | ||
| If you are using pyenv to handle your Python versions, you can simply use the instructions above, but pyenv does have a special way to handle virtual environments directly. | If you are using pyenv to handle your Python versions, you can simply use the instructions above, but pyenv does have a special way to handle virtual environments directly. | ||
| Ligne 79: | Ligne 144: | ||
| Further, let’s say that you want to use 3.11.7 for this project and you want to name the virtual environment “project1” . Your terminal command line would be… | Further, let’s say that you want to use 3.11.7 for this project and you want to name the virtual environment “project1” . Your terminal command line would be… | ||
| + | |||
| + | 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 | 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 | ||
| pyenv activate project1 | pyenv activate project1 | ||
| Ligne 94: | Ligne 182: | ||
| To deactivate the pyenv version of the environment, | To deactivate the pyenv version of the environment, | ||
| - | (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, | ||
| + | |||
| + | Well, that’s about it for this time. Until next time, as always; stay safe, healthy, positive and creative!** | ||
| - | Remember that when you create the pyenv virtual environment, you need to remember that the version | + | N' | 
| - | Well, that’s about it for this time. Until next time, as always; stay safe, healthy, positive and creative! | + | Eh bien, c'est à peu près tout pour cette fois. Jusqu' | 
issue208/python.1725107211.txt.gz · Dernière modification : 2024/08/31 14:26 de auntiee
                
                