If your project running on macOS requires an uninstalled version of Python, try PyEnv.
Even for experienced developers, managing the native Python development environment remains a challenge. Despite the detailed package management strategy, there are additional steps you need to take to make sure you run the Python version you need when you need it.
Why is the Python version important?
It’s a strange concept at first, but programming languages change just like any other software. They have bugs, fixes, and updates, just like your favorite API and any other software. Again, different distributions are identified by three-digit numbers called semantic versions.
😠😠😠pic.twitter.com/yt1Z2439W8
— Denny Perez (@DennyPerez18) May 28, 2019
For many years, Python 2 was the commonly-used major version of the language. In January 2020, Python 2 reaches its final life, after which Python core maintainers will only support Python 3. Python 3 is progressing steadily, with new updates released regularly. It’s important for me to get these updates regularly.
Recently, I tried to run a project on macOS that relied on Python 3.5.9, which was not installed on my system. I thought the Python package manager PIP could install it, but it didn’t:
$PIP install python3.5.9 Collecting python3.5.9 ERROR: Could not find a version that satisfies the requirement python3.5.9 (from versions: none) ERROR: No matching distribution foundfor python3.5.9
Copy the code
Alternatively, I can download the version from the official Python website, but how do I run it with the existing Python version on my Mac? Specifying the Python interpreter version (such as Python3.7 or Python3.5) every time you run it seems error-prone. There has to be a better way.
(Note: I know this doesn’t make sense to experienced Python developers, but it made sense to me at the time. I’d be happy to talk about why I still think it should.)
Install and set up PyEnv
Thankfully, PyEnv can bypass this complex set of issues. First, I need to install PyEnv. I can clone and compile it from source, but I prefer to manage packages through Homebrew package manager:
$ brew install pyenv
Copy the code
To use the Python version with PyEnv, you must know the shell’s PATH variable. PATH determines where the shell searches for files by command name. You must make sure that your shell can find the version of Python that runs through PyEnv, not the version installed by default (often called the system version). If you do not change the path, the result is as follows:
$ which python
/usr/bin/python
Copy the code
This is the system version of Python.
To set up Pyenv properly, run the following command in Bash or ZSH:
$ PATH=$(pyenv root)/shims:$PATH
Copy the code
Now, if you check the version of Python, you’ll see that it’s the Pyenv-managed version:
$ which python
/Users/my_username/.pyenv/shims/python
Copy the code
The export statement (PATH=) only makes changes to the shell instance, and you need to add it to the point file to make the changes permanent. Since ZSH is the default shell for macOS, I’ll focus on it. Add the same syntax to the ~/.zshrc file:
$ echo 'PATH=$(pyenv root)/shims:$PATH' >> ~/.zshrc
Copy the code
Now, every time we run a command in ZSH, it will use the Pyenv version of Python. Note that I use single quotes in Echo, so it doesn’t evaluate and extend commands.
The.zshrc file only manages ZSH instances, so be sure to check your shell program and edit the associated dot files. If you need to check the default shell program again, you can run echo $shell. If it is ZSH, use the command above. If you use Bash, change ~/.zshrc to ~/.bashrc. If you want to learn more, you can delve into path Settings in PyEnv’s README.
Manage Python versions using PyEnv
Now that PyEnv is available, we can see that it is only available in system Python:
$ pyenv versions
system
Copy the code
As mentioned above, you definitely don’t want to use this version (read more about it). Now that PyEnv is properly set up, I expect it to have several different versions of Python that I use regularly.
One way to see all Python versions in all repositories that PyEnv can access is by running Pyenv Install –list. This is a long list that may be helpful in future reviews. For now, I decide to go to the Python download page for each latest “dot version” I find (3.5.x or 3.6.x, where X is the latest). Therefore, I will install 3.5.9 and 3.8.0:
$PyEnv install 3.5.9 $Pyenv install 3.8.0Copy the code
This will take a while, so take a break (or read one of the links above). Interestingly, the output shows the download and build of this version of Python. For example, the output display file comes directly from Python.org.
Once installed, you can set the default values. I prefer the latest, so I set the global default Python version to the latest:
$pyenv global 3.8.0Copy the code
The version is immediately set up in my shell. Just to be sure:
$python -v python 3.8.0Copy the code
The project I’m running is only for Python 3.5, so I’ll set up the version locally and confirm:
$ pyenv local3.5.9 $python -v Python 3.5.9Copy the code
Because I used the local option in Pyenv, it adds a file to the current directory to keep track of this information.
$ cat .python-version
3.5.9
Copy the code
Now I can finally set up the virtual environment for the project I want and make sure I’m running the correct version of Python.
$ python -m venv venv
$ source ./venv/bin/activate
(venv) $ which python
/Users/mbbroberg/Develop/my_project/venv/bin/python
Copy the code
For more information, check out the tutorial on managing virtual environments on a Mac.
conclusion
Running multiple versions of Python can be a challenge by default. I have found that PyEnv ensures that I have the Version of Python I need when I need it.
Do you have any other beginner or intermediate Python questions? Please leave a comment and we’ll consider them in a future article.
Via: opensource.com/article/20/…
By Matthew Broberg, lujun9972
This article is originally compiled by LCTT and released in Linux China