From 834336c90c01e57d11292894b2990445b40a9a6c Mon Sep 17 00:00:00 2001 From: dino-vinkovic Date: Thu, 12 Dec 2024 20:28:14 +0100 Subject: [PATCH 1/7] Add Using pyenv article --- Automation/Python/Using pyenv.md | 172 +++++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 Automation/Python/Using pyenv.md diff --git a/Automation/Python/Using pyenv.md b/Automation/Python/Using pyenv.md new file mode 100644 index 0000000..832a590 --- /dev/null +++ b/Automation/Python/Using pyenv.md @@ -0,0 +1,172 @@ +## What is pyenv + +`pyenv` is a tool used to manage multiple Python versions on a single system. It helps to easily install, switch between, and manage different versions of Python. + + +## Why use it + +* **Avoid version conflicts**: Different projects may require different versions of Python. `pyenv` allows you to manage and isolate these versions, so you don’t run into compatibility issues. + +* **Easier testing**: You can test your code across multiple versions of Python without installing each version system-wide. + +* **Better control**: You can manage Python versions independently of the system's default Python, preventing changes that could affect system tools or applications that rely on a specific version of Python. + + +## Before you continue with pyenv installation + +Make sure [Homebrew](https://brew.sh/) is installed in the correct location. +If you have migrated from Mac using Intel architecture to one with ARM, you might have Homebrew still installed in the old location. + +Run: `which brew` + +Expected output: `/opt/homebrew` + +If your output is `/usr/local`, you should re-install Homebrew. + + +## How to install pyenv + +1. Install dependencies + * `brew install openssl readline sqlite3 xz zlib tcl-tk@8` + +2. Install pyenv using [pyenv-installer](https://github.com/pyenv/pyenv-installer) + * `curl https://pyenv.run | zsh` + * This should install `pyenv` and a few useful plugins like `pyenv-virtualenv` + +3. Update PATH in `/.zprofile`. Add the following: + ``` + export PYENV_ROOT="$HOME/.pyenv" + [[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH" + eval "$(pyenv init --path)" + eval "$(pyenv init -)" + eval "$(pyenv virtualenv-init -)" + ``` + +4. Reload the shell + * Run: `exec "$SHELL"` + * Or just restart the terminal + + +## How to install Python using `pyenv` + +1. Run the following to check available CPython versions: + * `pyenv install --list` + * This returns quite a few, including Python 2 + * `pyenv install --list | grep -E '^ 3\.[0-9]+'` + * This returns a list of available CPython 3 versions + +2. Install a specific Python version, e.g. 3.11.11: + * `pyenv install -v 3.11.11` + + +### Where is Python installed + +Each version is installed in pyenv root directory. + +To check all installed versions, run: `ls ~/.pyenv/versions/` + + +## How to set Python version + +### Check available versions + +Run `pyenv versions` to see which versions you have available. +The `*` indicates which version is currently active. By default it is the system's version. + +```sh +➜ ~ pyenv versions + * system (set by /Users/username/.pyenv/version) + 3.9.7 + 3.11.11 +``` + +### Set system Python version + +To switch the version you want to use for the system, use the `global` command. + +```sh +➜ ~ pyenv global 3.11.11 + system + 3.9.7 + * 3.11.11 (set by /Users/username/.pyenv/version) +``` + +Run `python -V` to check if the correct version is applied. + +```sh +➜ ~ python -V +Python 3.11.11 +``` + +To change it back to default system version + +```sh +➜ ~ pyenv global system +``` + +## How to uninstall a particular Python version + +You can uninstall a Python version using pyenv uninstall, or simply delete the directory to remove a specific Python version. + +**Uninstall:** + +```sh +➜ ~ pyenv uninstall 3.11.11 +``` + + +**Remove:** + +```sh +➜ ~ rm -rf ~/.pyenv/versions/3.11.11 +``` + +## How to set virtual environment with pyenv + +`pyenv` is primarily a tool for managing Python versions. But it also supports plugins. +One of those supported plugins is `pyenv-virtualenv`, which helps you manage virtual environments for specific Python versions. + +### Create virtual environment + +When creating a virtual environment, try to use a name that would easily associate it with the project you are working on. Just to make things convenient. + +```sh +➜ ~ pyenv virtualenv 3.11.11 my_project_venv +``` + +### Activate virtual environment + +```sh +➜ ~ pyenv local my_project_venv +``` + +This creates a `.python-version` file in the current working directory, which automatically activates the virtual environment whenever you are in that directory. + +```sh +➜ ~ pyenv which python +/Users/username/.pyenv/versions/my_project_venv/bin/python +``` + +### Remove virtual environment + +To remove the local version setting, you can do one of the following: + +Delete the `.python-version` file in your directory: + +```sh +➜ ~ rm .python-version +``` + +Or use the `--unset` command to remove the local version setting: + +```sh +➜ ~ pyenv local --unset +``` + +Once the `.python-version` file is removed or reset, the system will use the global Python version. + + +### Additional resources + +[pyenv] https://github.com/pyenv/pyenv +[Managing Multiple Python Versions With pyenv](https://realpython.com/intro-to-pyenv/) From 0dffc7ce140a3f71ae1434cdeff19c0f1dda6d9b Mon Sep 17 00:00:00 2001 From: dino-vinkovic Date: Thu, 12 Dec 2024 20:38:55 +0100 Subject: [PATCH 2/7] Improve code blocks --- Automation/Python/Using pyenv.md | 38 +++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/Automation/Python/Using pyenv.md b/Automation/Python/Using pyenv.md index 832a590..1aa8c01 100644 --- a/Automation/Python/Using pyenv.md +++ b/Automation/Python/Using pyenv.md @@ -27,13 +27,20 @@ If your output is `/usr/local`, you should re-install Homebrew. ## How to install pyenv 1. Install dependencies - * `brew install openssl readline sqlite3 xz zlib tcl-tk@8` + + ```sh + ➜ ~ brew install openssl readline sqlite3 xz zlib tcl-tk@8 + ``` 2. Install pyenv using [pyenv-installer](https://github.com/pyenv/pyenv-installer) - * `curl https://pyenv.run | zsh` - * This should install `pyenv` and a few useful plugins like `pyenv-virtualenv` + * This should install `pyenv` and a few useful plugins like `pyenv-virtualenv` + + ```sh + ➜ ~ curl https://pyenv.run | zsh + ``` 3. Update PATH in `/.zprofile`. Add the following: + ``` export PYENV_ROOT="$HOME/.pyenv" [[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH" @@ -42,21 +49,30 @@ If your output is `/usr/local`, you should re-install Homebrew. eval "$(pyenv virtualenv-init -)" ``` -4. Reload the shell - * Run: `exec "$SHELL"` - * Or just restart the terminal +4. Restart the terminal ## How to install Python using `pyenv` 1. Run the following to check available CPython versions: - * `pyenv install --list` - * This returns quite a few, including Python 2 - * `pyenv install --list | grep -E '^ 3\.[0-9]+'` - * This returns a list of available CPython 3 versions + + ```sh + ➜ ~ pyenv install --list + ``` + + * This returns quite a few, including Python 2 + + ```sh + ➜ ~ pyenv install --list | grep -E '^ 3\.[0-9]+' + ``` + + * This returns a list of available CPython 3 versions 2. Install a specific Python version, e.g. 3.11.11: - * `pyenv install -v 3.11.11` + + ```sh + ➜ ~ pyenv install -v 3.11.11 + ``` ### Where is Python installed From ad1c45cc2d0947591dfc3cf3ea19f9f8d9352e0a Mon Sep 17 00:00:00 2001 From: dino-vinkovic Date: Thu, 12 Dec 2024 20:41:18 +0100 Subject: [PATCH 3/7] Fix code block --- Automation/Python/Using pyenv.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Automation/Python/Using pyenv.md b/Automation/Python/Using pyenv.md index 1aa8c01..0d14a68 100644 --- a/Automation/Python/Using pyenv.md +++ b/Automation/Python/Using pyenv.md @@ -58,15 +58,13 @@ If your output is `/usr/local`, you should re-install Homebrew. ```sh ➜ ~ pyenv install --list + // Returns quite a few, including Python 2 ``` - * This returns quite a few, including Python 2 - ```sh ➜ ~ pyenv install --list | grep -E '^ 3\.[0-9]+' + // Returns a list of available CPython 3 versions ``` - - * This returns a list of available CPython 3 versions 2. Install a specific Python version, e.g. 3.11.11: From 9ab01c1a91e501bc0006ab854bf2c968bbb893a0 Mon Sep 17 00:00:00 2001 From: dino-vinkovic Date: Fri, 13 Dec 2024 07:46:01 +0100 Subject: [PATCH 4/7] Explicitly mention CPython --- Automation/Python/Using pyenv.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Automation/Python/Using pyenv.md b/Automation/Python/Using pyenv.md index 0d14a68..1b92d29 100644 --- a/Automation/Python/Using pyenv.md +++ b/Automation/Python/Using pyenv.md @@ -58,7 +58,7 @@ If your output is `/usr/local`, you should re-install Homebrew. ```sh ➜ ~ pyenv install --list - // Returns quite a few, including Python 2 + // Returns quite a few, including CPython 2 ``` ```sh @@ -66,7 +66,7 @@ If your output is `/usr/local`, you should re-install Homebrew. // Returns a list of available CPython 3 versions ``` -2. Install a specific Python version, e.g. 3.11.11: +2. Install a specific CPython version, e.g. 3.11.11: ```sh ➜ ~ pyenv install -v 3.11.11 From e445082af629c9374a4cc68bc265448a47cf9348 Mon Sep 17 00:00:00 2001 From: dino-vinkovic Date: Fri, 13 Dec 2024 07:56:01 +0100 Subject: [PATCH 5/7] Update indentation --- Automation/Python/Using pyenv.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Automation/Python/Using pyenv.md b/Automation/Python/Using pyenv.md index 1b92d29..2597997 100644 --- a/Automation/Python/Using pyenv.md +++ b/Automation/Python/Using pyenv.md @@ -89,7 +89,7 @@ The `*` indicates which version is currently active. By default it is the system ```sh ➜ ~ pyenv versions - * system (set by /Users/username/.pyenv/version) + * system (set by /Users/username/.pyenv/version) 3.9.7 3.11.11 ``` @@ -102,7 +102,7 @@ To switch the version you want to use for the system, use the `global` command. ➜ ~ pyenv global 3.11.11 system 3.9.7 - * 3.11.11 (set by /Users/username/.pyenv/version) + * 3.11.11 (set by /Users/username/.pyenv/version) ``` Run `python -V` to check if the correct version is applied. From bc9660e29548613c7b592b4d2557e660b6b05032 Mon Sep 17 00:00:00 2001 From: dino-vinkovic Date: Fri, 13 Dec 2024 08:13:43 +0100 Subject: [PATCH 6/7] Add link to pyenv article, minor text improvements --- Automation/Python/Virtual Environment.md | 48 +++++++++++++++--------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/Automation/Python/Virtual Environment.md b/Automation/Python/Virtual Environment.md index 2f8e364..f402db2 100755 --- a/Automation/Python/Virtual Environment.md +++ b/Automation/Python/Virtual Environment.md @@ -10,6 +10,10 @@ Each of those projects can also have different packages installed, different ver If you ever decide to change the Python version or dependencies in one of your projects, you don’t have to worry about those changes affecting other projects. +NOTE: + +* The article explains how to manage virtual environments using the **venv** module from the standard Python library, and PyCharm. +* For info on how to manage Python versions and virtual environments with `pyenv`, check the [Using pyenv](https://infinum.com/handbook/qa/automation/python/using-pyenv) article. ### How to install @@ -20,12 +24,16 @@ If you are using Python 3, then you should already have the **venv** module from From Python 3.6 and above, the recommended way to create a virtual environment is using the following command: -`python3 -m venv` +```sh +➜ ~ python3 -m venv +``` To the command above, simply add the name you want your virtual environment to have. Let’s say *env*. Running the following command creates a virtual environment called *env*: -`python3 -m venv env` +```sh +➜ ~ python3 -m venv env +``` When created, the virtual environment shows up as a directory in your project. The directory contains Python executable files and other installed libraries. @@ -34,9 +42,11 @@ When created, the virtual environment shows up as a directory in your project. T ### How to activate Virtual Environment? -To use the virtual environment, you have to activate it. The virtual environment is activated by running the following command: +To use the virtual environment, you have to activate it. The virtual environment is activated by running the `activate` command: -`source env/bin/activate` +```sh +➜ ~ source env/bin/activate +``` Once activated, you’ll notice the (env) prefix in the terminal, indicating that the env is active. Now you can add dependencies that will be used for this project only. @@ -45,9 +55,11 @@ Once activated, you’ll notice the (env) prefix in the terminal, indicating tha ### How to deactivate Virtual Environment? -To deactivate the virtual environment and use the system / global settings, run the following command: +To deactivate the virtual environment and use the system / global settings, run the `deactivate` command: -`deactivate` +```sh +➜ ~ deactivate +``` Now the (env) prefix is no longer displayed in the terminal. Any Python command you’d now run would refer to the global Python. @@ -58,8 +70,8 @@ The interpreter translates your code into the language the computer hardware und You can either use a system interpreter that comes with Python installation or you can configure a virtual environment that takes a system interpreter but can be further modified for your project. NOTE: -Make sure you have the desired Python version installed. -When configuring a Python interpreter, you have to specify the path to the Python executable in your system. + +* Make sure you have the desired Python version installed. When configuring a Python interpreter, you have to specify the path to the Python executable in your system. ---- @@ -75,14 +87,14 @@ When creating a new project from scratch, the virtual environment named *venv* ( - If an existing project opens, select *File -> New project…* 3. In the *Location* field type in the path to the project with the project folder name at the end, e.g.: - - Mac: /Users/username/Documents/Code/myPythonProject - - Windows: D:\Code\myPythonProject + - Mac: `/Users/username/Documents/Code/myPythonProject` + - Windows: `D:\Code\myPythonProject` 4. If not selected by default, select the *New environment using* button. Next to it, the option *Virtualenv* should be selected 5. The Location field in the Python Interpreter section should look like this: - - Mac: /Users/username/Documents/Code/myPythonProject/venv - - Windows: D:\Code\myPythonProject\venv + - Mac: `/Users/username/Documents/Code/myPythonProject/venv` + - Windows: `D:\Code\myPythonProject\venv` 6. In the *Base interpreter* field select a path to a Python executable in your system. As shown in the image below, Python 3.9 was selected as the base interpreter for the project. @@ -95,8 +107,8 @@ When creating a new project from scratch, the virtual environment named *venv* ( ## Create & activate virtual environment for an existing project -When starting your work on an existing project, you have to activate the virtual environment through the terminal. You will also have to select the desired interpreter in the settings. -For Windows, let’s imagine we have cloned a project named *myPythonProject* to *D:\Code* +When starting your work on an existing project, you have to activate the virtual environment through the terminal. You also have to select the desired interpreter in the settings. +For Windows, let’s imagine we have cloned a project named `myPythonProject` to `D:\Code`. 1. Open the project and position to the project root folder @@ -143,12 +155,14 @@ Or you can omit the sign completely which will install the latest stable version #### How to install the required dependencies -Make sure you have the virtual environment active and the requirements.txt file ready. +Make sure you have the virtual environment active and the `requirements.txt` file ready. -1. Open the terminal and position to the folder where the *requirements.txt* file is +1. Open the terminal and position to the folder where the `requirements.txt` file is 2. Run the following command: -`pip install -r requirements.txt` +```sh +➜ ~ pip install -r requirements.txt +``` You should now have all the dependencies specified in the requirements.txt file installed for the current project. From eb44cffe4bfd35eda7e20ade7034d3749491e039 Mon Sep 17 00:00:00 2001 From: dino-vinkovic Date: Tue, 17 Dec 2024 07:28:43 +0100 Subject: [PATCH 7/7] Update brew location --- Automation/Python/Using pyenv.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Automation/Python/Using pyenv.md b/Automation/Python/Using pyenv.md index 2597997..29ccfd8 100644 --- a/Automation/Python/Using pyenv.md +++ b/Automation/Python/Using pyenv.md @@ -17,9 +17,10 @@ Make sure [Homebrew](https://brew.sh/) is installed in the correct location. If you have migrated from Mac using Intel architecture to one with ARM, you might have Homebrew still installed in the old location. -Run: `which brew` - -Expected output: `/opt/homebrew` +```sh +➜ ~ which brew +/opt/homebrew/bin/brew +``` If your output is `/usr/local`, you should re-install Homebrew.