- Summary
- Install
pyenv
andpoetry
- Start a project with
pyenv
andpoetry
- Open a project with
pyenv
andpoetry
- Manage dependencies
- Next Steps
- Virtual environments are independent installations of Python and packages.
- They are used for managing dependencies that might be specific to your project.
- For instance, your project might need a specific version of Python or a specific Python package. Virtual environments allow you to have a contained instance of the right version of Python with the right version packages, to make running your project easier.
- Pyenv is a virtual environment tool, that focuses specifically on managing different versions of Python in your system.
- Pyenv is useful to run projects that have been designed around a specific version of Python, and might not work on other versions.
- In such a case, you would use
pyenv
to install and use a specific version of Python.
- Poetry is another virtual environment tool that focuses more on package managing.
- If only certain version packages work on your project (i.e. a specific version of
numpy
or a specific version ofPandas
, etc.), it's a great tool that can be used to define specific project dependencies. - By installing Poetry in your different machines, you will be able to very easily install every single package required for the project by running a simple
poetry install
command in the project directory. - If you have the right version of Python installed in your machine (with
pyenv
, for example), then runningpoetry shell
will open up a shell with a virtual environment that has the right version of Python and the right packages.
- Before getting started, you will first need to allow running scripts in your Shell.
- Open Windows PowerShell as an administrator.
- Run:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
. - Select yes
[Y]
to allow scripts to be run in your machine.
Note
You can change this back to no [N]
after having installed the packages, for security reasons.
- Run:
Invoke-WebRequest -UseBasicParsing -Uri "https://raw.githubusercontent.com/pyenv-win/pyenv-win/master/pyenv-win/install-pyenv-win.ps1" -OutFile "./install-pyenv-win.ps1"; &"./install-pyenv-win.ps1"
. - Close and open the terminal (also close and open your IDE).
- Run
pyenv --version
to check that the installation was successful and that you have a version of pyenv in your machine.
Note
For pyenv to work properly, you might have to uninstall other Python environment managers if you have any installed.
- Before installing poetry, you will need a version of Python. Any version will do. We will use pyenv to download one and set is as the global system version.
- Run:
pyenv install -l
. This will display a list of all available versions of Python to install. - Run:
pyenv install 3.11.4
.
[!NOTE] At the time of writing, the latest stable build is
3.11.4
, so we will install this one. You can install any version, however.- Now set is as the global default, for this, run:
pyenv global 3.11.4
. - Verify this is set correctly, run:
pyenv versions
, make sure3.11.4
is there and is highlighted with an asterisk*
, denoting it as the global Python installation.
- Run:
Note
Now, when you type python
in any command window in your system, a new Python 3.11.4
shell will open. You can close it by typing exit()
.
This is the version that will run by default in any directory if you have not selected a local version.
-
Run:
(Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | python -
. -
We now need to add Poetry to our
PATH
, to make sure it can be called everywhere. To do so:- On install, Poetry will give you it's
bin
directory and prompt you to add it to yourPATH
.
[!NOTE] At the time of writing, for me, it was
C:\Users\YOUR_USER_NAME\AppData\Roaming\Python\Scripts
, but keep in mind that this might change over time, so you should always pick the one that was given to you upon install.- On the windows search bar, type
Environment Variables
and click onEdit the system environment variables
. - Click on
Environment Variables...
. - Under
System Variables
, selectPath
. - Click on
Edit...
. - In the new window that opened, click on
New
. Paste whatever path poetry gave you on installation. - Click on
Ok
on both windows. - Restart your machine.
- Run
poetry --version
to check that the installation was successful and that you have a version of Poetry in your machine.
- On install, Poetry will give you it's
Important
It is highly recommended you run this option change now: poetry config virtualenvs.in-project true
. This will prompt Poetry to install your virtual environment in the project directory itself, rather than on your machine. This virtual environment is still specific to your machine and will be ignored by git. There are advantages and drawbacks to enabling this option. One of the advantages is that, when deleting the project directory, you will delete all the dependencies with it, thus not cluttering your machine with virtual environments.
- Refer to the following guides:
-
cd
to your project directory. - Make sure you have installed the version of Python you want to use for your project: run
pyenv versions
.
Note
Else, run pyenv install -l
for a list of available Python versions, and pyenv install THE_PYTHON_VERSION
to install your desired version (change THE_PYTHON_VERSION
for your desired version, such as 3.11.4
or similar).
- We can now run
poetry init
. This interactive initialization procedure will guide you through adding your initial Python version and package dependencies. - You can now run
poetry shell
to start a Python shell. Poetry will first create a virtual environment (it will do so in the project directory if you set the option prior). To do so, it will look for whichever version of Python you specified when running the initialization procedure, and if it is not in your system, will prompt you to install it (you can use pyenv). Then, once it finds the version of Python, it will start a new shell with your virtual environment. - You can add and install dependencies with
poetry add PACKAGE_NAME@*
. You can changePACKAGE_NAME
for your specific package name. In case on package name ambiguity, poetry will prompt you to select the desired package. The trailing@*
is not necessary, and it is used to specify a version. For instance,poetry add [email protected]
will installnumpy 11.2
and add it to your dependency file. By leaving an asterisk@*
you are specifying the latest version that is compatible with all other dependencies, thus making it a good practice to use it.
- Open a PowerShell terminal in your project directory.
- Make sure you have installed the version of Python you want to use for your project: run
pyenv versions
.
Note
Else, run pyenv install -l
for a list of available Python versions, and pyenv install THE_PYTHON_VERSION
to install your desired version (change THE_PYTHON_VERSION
for your desired version, such as 3.11.4
or similar).
- We can now run
poetry init
. This interactive initialization procedure will guide you through adding your initial Python version and package dependencies. - You can now run
poetry shell
to start a Python shell. Poetry will first create a virtual environment (it will do so in the project directory if you set the option prior). To do so, it will look for whichever version of Python you specified when running the initialization procedure, and if it is not in your system, will prompt you to install it (you can use pyenv). Then, once it finds the version of Python, it will start a new shell with your virtual environment. - You can add and install dependencies with
poetry add PACKAGE_NAME@*
. You can changePACKAGE_NAME
for your specific package name. In case on package name ambiguity, poetry will prompt you to select the desired package. The trailing@*
is not necessary, and it is used to specify a version. For instance,poetry add [email protected]
will installnumpy 11.2
and add it to your dependency file. By leaving an asterisk@*
you are specifying the latest version that is compatible with all other dependencies, thus making it a good practice to use it.
-
cd
to your project directory.
Note
If you still need to clone the repo, you can gh repo clone
. For more information, visit 5.1 - Using GitHub.
- If your project already has a poetry environment ready, you can use
poetry shell
to attempt to launch a shell in your virtual environment.
Warning
Poetry might warn you to install the right version of Python if you haven't already. To do so, use Pyenv: pyenv install VERSION_OF_PYTHON
(replace VERSION_OF_PYTHON
by the version suggested by Poetry).
- Once in the shell, use
poetry install
to install all the right dependencies. - That's it! You can now execute your project from the shell having all the right dependencies.
- Open or clone your project using VS Code.
- Open a terminal PowerShell instance on your project.
- If your project already has a poetry environment ready, you can use
poetry shell
to attempt to launch a shell in your virtual environment.
Warning
Poetry might warn you to install the right version of Python if you haven't already. To do so, use Pyenv: pyenv install VERSION_OF_PYTHON
(replace VERSION_OF_PYTHON
by the version suggested by Poetry).
- Once in the shell, use
poetry install
to install all the right dependencies. - That's it! You can now execute your project from the shell having all the right dependencies.
Additionally, you should also set your Poetry virtual environment as the default interpreter for your project, to be able to run it straight without starting a poetry shell.
- To do so, use
Ctrl + Shift + P
to open the VS Code command bar, and typePython Interpreter
. Select the local.venv
poetry interpreter (which should appear after you have opened apoetry shell
at least once, as Poetry will automatically install the virtual environment as you do so). - That's it! You can now execute your project directly from VS Code having all the right dependencies.
- Start to manage a repo with Poetry with
poetry init
. - You can add a library with
poetry new <library>@*
. - You can remove a library with
poetry remove <library>
. - Run outside of Poetry Shell with
poetry run python app.py
. - Show dependencies with
poetry show
. - Lock dependencies with
poetry lock
. - Spawn a shell in the project's virtual environment with
poetry shell
. - Update dependencies with
poetry update
.
That is it for this guide! You are now ready to start a project by creating a Python file!