Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New article: "Using pyenv" #202

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
187 changes: 187 additions & 0 deletions Automation/Python/Using pyenv.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
## 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.

```sh
➜ ~ which brew
/opt/homebrew/bin/brew
```

If your output is `/usr/local`, you should re-install Homebrew.


## How to install pyenv

1. Install dependencies

```sh
➜ ~ brew install openssl readline sqlite3 xz zlib tcl-tk@8
```

2. Install pyenv using [pyenv-installer](https://github.com/pyenv/pyenv-installer)
* 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:
dino-vinkovic marked this conversation as resolved.
Show resolved Hide resolved

```
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. Restart the terminal


## How to install Python using `pyenv`

1. Run the following to check available CPython versions:

```sh
➜ ~ pyenv install --list
// Returns quite a few, including CPython 2
```

```sh
➜ ~ pyenv install --list | grep -E '^ 3\.[0-9]+'
// Returns a list of available CPython 3 versions
```

2. Install a specific CPython version, e.g. 3.11.11:

```sh
➜ ~ 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/)
48 changes: 31 additions & 17 deletions Automation/Python/Virtual Environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.

Expand All @@ -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.

Expand All @@ -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.

Expand All @@ -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.


----
Expand All @@ -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.

Expand All @@ -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

Expand Down Expand Up @@ -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.