pyenv local - can't set a Python version by its number despite the fact that it's installed

12,057

Solution 1

You need to set up an virtual env first. For example, you can create a env called pyenv360 by

$ pyenv virtualenv 3.6.0 pyenv360

And set it as your default python for your folder my-folder by

$ cd my-folder
$ pyenv local pyenv360

By doing this, whenever you enter this folder, it'll start using pyenv360 which references python 3.6.0 in this case.

You can check if it's working by:

$ pyenv local

which should show pyenv360.

Solution 2

(Assuming you installed pyenv according to the official instructions.)

pyenv recognizes two kinds of Python installations:

  • system -- Python executables from PATH, regardless of their versions
  • numbered versions -- installed with pyenv install under $PYENV_ROOT/versions

pyenv considers the former installations outside its control, so its commands don't take their versions into account when looking for a specific version.


To find out which installation, from pyenv's POV, your python3 refers to, check

  • pyenv versions
    • whether the selected Python 3 version is "system"; or
  • which python3 (and pyenv which python3 if the former points into <pyenv root>/shims/)
    • whether the result points to outside $PYENV_ROOT

If your 3.6.0 installation is indeed outside of pyenv's control, you need to select it with pyenv local system and make sure on your own that your PATH is arranged to point to it.

  • More specifically, that your PATH first points to <pyenv_root>/shims and then to the correct python3
    • You can use which -a python3 to quickly check that

Solution 3

Overview

'pyenv' has the feature to set the 'local' and the 'global' version,

where 'local' is the version that we set for a particular directory and it automatically gets activated if you are in that directory or any of its subdirectory(if another 'local' is not set for that subdirectory) in a hierarchical fashion.

whereas 'global' is the version that we set for all over the system and it is available to any directory(if the local is not set for that directory otherwise the local version would be available in that directory)

How to set 'global' and 'local'

now to set 'global' or the 'local' version of the python, that python version should be installed in your system, be it by the 'os' you used or by the 'pyenv'. And there is no need of virtual environment creation at all.

To Install any version via 'pyenv'

see the output of pyenv install --list choose one of the name(eg. 3.6.0) and use command pyenv install <version-name>

Note that the version of python that is installed by the os is called 'system' by the 'pyenv' and the versions that are installed by the 'pyenv' would be represented by the version number of that python version. to see all the versions installed by the pyenv use pyenv versions

Now coming to the question of how to set the global version and the local version, use

pyenv global <version-name-as used-by-pyenv> (the version has to be installed) to set the global version

and for setting the local python, first, move to the directory in which you want to set the local version, and then

pyenv local <version-name-as used-by-pyenv>

Issue that I faced in ubuntu 20.04, and in Linux mint 20

Now sometimes the 'system' python is not accessible due to its unreachability by 'pyenv' and the reason is well explained by @ivan_pozdeev, but I would like to address the wired issue that I face in ubuntu 20.04 and Linux mint 20(as it's based on ubuntu 20.04).

Here I am not able to access the system python, even though the system python binaries are well in the path of pyenv.

the error was pyenv: system version not found in PATH

reason: the 'system' is not found by the pyenv, because pyenv was looking for binaries with name 'python' and not 'python3' in the path(which is '/usr/bin'), and in ubuntu 20.04 the python binaries are addressed with name 'python3', and not with 'python'

solution: the solution is to create the symlink for 'python3' named as 'python' in '/usr/bin' and the command used is sudo ln -s /usr/bin/python3 /usr/bin/python

Share:
12,057
Snowcrash
Author by

Snowcrash

Updated on July 05, 2022

Comments

  • Snowcrash
    Snowcrash almost 2 years

    Any suggestions on how you set the version of Python for pyenv?

    E.g.

    $ python3
    Python 3.6.0 (v3.6.0:41df79263a11, Dec 22 2016, 17:23:13) 
    $ pyenv local
    pyenv: no local version configured for this directory
    $ pyenv local 3.6.0
    pyenv: version `3.6.0' not installed
    $ pyenv local v3.6.0:41df79263a11
    pyenv: version `v3.6.0' not installed