pyenv¶
TL;DR: Python versions come out all the time, and will break your code. If you want to reuse it in 2 years, use pyenv
pyenv
is a way of controlling Python versions. pyenv
, when set up correctly, will also let you set up VirTuaL EnVirOnMenTs!. You might prefer using conda
, which is absolutely fine! pyenv
is a personal preference for me, I think it has easier to remember commands.
If you have one tool that will only play nice with Python 2.7, and one that requries Python 3.6, you can use pyenv
to be able to use both on one machine.
I'm sold what do I do¶
This tutorial is better than anything I could write but I will sum it up https://realpython.com/intro-to-pyenv/
1. Get the Python version you want installed.¶
Pick a Python version, and install it. This is "downloading" it and getting it set up ready to use.
pyenv install 3.8-dev
2. Set up the Python version you want to use where¶
Now you need to activate the version where you're going to use it. If you want to use this version everywhere by default, use
pyenv global 3.8-dev
If you want it just in this directory (super handy), set it with
pyenv local 3.8-dev
You can use it just in this terminal, just for now with
pyenv shell 3.8-dev
3. Use Python like normal¶
Now you should be able to use Python however you normally would!
What's a virtual environment¶
Virtual environments are essentially like setting up a small space in your computer, that is brand new. No installs, no libraries, no Python. A virtual environment means that you can develop or use a package in a clean space, and set up exactly the libraries with all the versions you like. You can have as many virtual environments as you like.
Note: you might be interested in poetry
as well or instead
How should I use them?¶
If you're diligent, you might have a new virtual environment for every "project" you work on. Sometimes I forget to set a new one up.
I tend to have a default environment which has the Python version and libraries I like, and use everyday. When I'm making a new tool, I sometimes make a new environment for it, especially if I need to have a new Python version (e.g. 3.8+)
What do I do?¶
You can make a new virtual environment using
pyenv virtualenv <python_version> <environment_name>
e.g.
pyenv virtualenv 3.6.8 myproject
Then you can activate it just like above
pyenv local myproject