I have installed pyenv in my windows system and updated it using pyenv update. When I run pyenv --version it shows 2.64.11. I installed python 3.9.9 to use in some project using command pyenv install 3.9.9. Now I open my project in visual studio code and open terminal and run the some above commands like for checking version of pyenv and pyenv versions which shows me 3.9.9 in output.Now I am in one of the directory of the D:/notification folder. Now I run pyenv local 3.9.9 and check by typing pyenv versions which shows me *3.9.9 which means 3.9.9 version is set by pyenv for this directory and for its child directories. But in meantime if I run command python --version it shows me 3.8.3 result and this is the version which I have installed from the official python.org website. I don't know why it is behaving like that. When I move to one more directory down i.e. D:/notification/notification then also the above stated problem remains as it is. Please help me in fixing this issue.
Related
Background:
In WSL2 (ubuntu 20.04) I created a python virtual environment inside a directory. Using the command python3 -m venv venv my system's python version was set to python3.11 (after downloading) via sudo update-alternatives --config python3 and then choosing the version. I noticed I was having some errors of missing modules when I started WSL2 (happening after a computer restart), I read this was because I was using a different python version than the one ubuntu 20.04 came with so I switched back to 3.8 via the config menu as before. I am also using VS code that's connected to my WSL2.
These are some of the contents of my venv directory: venv/bin/python venv/bin/python3 venv/bin/python3.11 venv/bin/pip venv/bin/pip3
Question:
After activating my virutal env via source venv/bin/activate, when I do python3 --version I still get a version of 3.8.10 despite creating the virtual environment with 3.11. I was able to get the interpretor set to 3.11 on VS code.I know I was in the virtual environment since my command prompt had (venv) in front. I went into the python console while in the virtual env and did import sys and sys.path this was my output ['', '/usr/lib/python38.zip', '/usr/lib/python3.8', '/usr/lib/python3.8/lib-dynload']. Why isn't the python version changing, am I misunderstanding something or did I not do something correctly? Seems like pip isn't working either but works when I switch my system python to 3.11 (I tried installing it on 3.8 but it said it was already installed).
Solved:
Answered below, just re-created the virtual env while making sure my system python version was 3.11 (may have been some mixup earlier).
By changing the selection in sudo update-alternatives --config python3 you change the selected python version also for the chosen vitrual environment (at least when using venv, it might be different with other tools).
That can cause issues, because when creating a new virtual environment envname using venv from a specific python version xx.xx, a directory named pythonxx.xx is created in /envname/lib/, and inside it a directory named site-packages that contains the packages installed by the pip of this specific environment.
So changing back to the original python version of the environment through sudo update-alternatives --config python3 should solve the issue, and probably the errors of missing modules are due to the incompatibility of the current selected python version with the original version which you installed the virtual environment from.
Personally, to avoid confusing, I name my virtual environments with the python version as a suffix, e.g envname_py3.11.1. But there might be a better method which I am not aware of.
I deleted my venv directory and recreated my virtual environment while on python3.11. This has resolved my issue.
I have Python 3.9.6 installed on my machine, along with 3.6.5, the two of which I use for different projects. At least that is the idea. However, when I run the command pyenv global 3.9.6, I get the response 3.9.6 not installed,
I do find python#3.9/3.9.6 in my Library folder. What's going on here, what's a good way to switch environments please?
Try to use pyenv versions in order to identify the Versions Installed.
If it is not installed you can use pyenv install.
To ask your question:
-Use pyenv install -l in order to see the curent available version that can be installed. Python 3.9.6 it is not one of them!
You did not install with pyenv Python 3.9.6, it cannot be installed with pyenv. That is the reason you cannot use it.
So currently, I have ubuntu 19. And it comes by default with python 3.7.5. I need to downgrade to 3.6.5.
EDIT:
I am using virtualenv
The following talks about upgrade from 3.6.7 to 3.7.0 but you can use the same process for downgrade. You should not change the system python unless you really know what you're doing
First Install Pyenv
Installlation Instructions are here
Look at Pyenv Options
$ pyenv
pyenv 1.2.14
Usage: pyenv <command> [<args>]
Some useful pyenv commands are:
commands List all available pyenv commands
activate Activate virtual environment
commands List all available pyenv commands
deactivate Deactivate virtual environment
doctor Verify pyenv installation and deevlopment tools to build pythons.
exec Run an executable with the selected Python version
global Set or show the global Python version
help Display help for a command
hooks List hook scripts for a given pyenv command
init Configure the shell environment for pyenv
install Install a Python version using python-build
local Set or show the local application-specific Python version
prefix Display prefix for a Python version
rehash Rehash pyenv shims (run this after installing executables)
root Display the root directory where versions and shims are kept
shell Set or show the shell-specific Python version
shims List existing pyenv shims
uninstall Uninstall a specific Python version
--version Display the version of pyenv
version Show the current Python version and its origin
version-file Detect the file that sets the current pyenv version
version-name Show the current Python version
version-origin Explain how the current Python version is set
versions List all Python versions available to pyenv
virtualenv Create a Python virtualenv using the pyenv-virtualenv plugin
virtualenv-delete Uninstall a specific Python virtualenv
virtualenv-init Configure the shell environment for pyenv-virtualenv
virtualenv-prefix Display real_prefix for a Python virtualenv version
virtualenvs List all Python virtualenvs found in `$PYENV_ROOT/versions/*'.
whence List all Python versions that contain the given executable
which Display the full path to an executable
See `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv/pyenv#readme
Look at Python Versions
$ pyenv versions
system
* 3.6.7 (set by /home/taarimalta/.pyenv/version)
Install a new Python
$ pyenv install 3.7.0
Installing Python-3.7.0...
WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib?
WARNING: The Python readline extension was not compiled. Missing the GNU readline lib?
WARNING: The Python sqlite3 extension was not compiled. Missing the SQLite3 lib?
Installed Python-3.7.0 to /home/taarimalta/.pyenv/versions/3.7.0
If you run into an issue with _ctypes install libffi-dev library
Now look at the versions
$ pyenv versions
system
* 3.6.7 (set by /home/taarimalta/.pyenv/version)
3.7.0
Select 3.7.0 for local environment
$ pyenv local 3.7.0
See that the version changed
$ pyenv versions
system
3.6.7
* 3.7.0 (set by /home/taarimalta/.python-version)
$ python
Python 3.7.0 (default, Jan 1 2020, 10:52:57)
[GCC 9.2.1 20191008] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
Switch to a different folder
cd ../project2
pyenv versions
system
* 3.6.7 (set by /home/taarimalta/.pyenv/version)
3.7.0
The python version may be different here depending on which python version you have set locally
Set pyenv version globally
This globally sets a python version for a user
pyenv global 3.7.0
Note that pyenv sets local version by adding a .python-version file
$ pyenv local 3.7.0
$ cat .python-version
3.7.0
Note that pyenv knows the global version by looking at the ~/.pyenv/version file
cat ~/.pyenv/version
3.8.2
Install it from the pre-compiled Ubuntu deb package repos.
$ sudo apt update
$ sudo apt install software-properties-common
$ sudo add-apt-repository ppa:deadsnakes/ppa
When prompted, pres ENTER to continue.
sudo apt install python3.6
Alternatively, you can easily create a virtual environment.
Suppose you have python 3.8 (or higher) installed on the system, but for a specific task, you need python 3.7 (or lower). The best idea is to Create a virtual environment with python 3.7(or any 3.x, change the commands below according to your desired version. Below is an implementation of a virtual environment with python 3.7)
Steps: (Checked August 2022)
Install python 3.7 and it’s virtual environment packages.
sudo apt-get install python3.7-dev python3.7-venv
Find out where your python 3.7 is located by this command:
which python3.7 (Should be something like /usr/bin/python3.7, if not found, then install python 3.7 manually)
Create Virtual Environment in the Home directory.
cd
mkdir virtual_env
/usr/bin/python3.7 -m venv ~/virtual_env/venv_with_python3.7
source ~/virtual_env/venv_with_python3.7/bin/activate
python --version (Should be python 3.7 now)
Done. Python 3.7 can be used in this virtual environment. Type which python, and you’ll see you have created python 3.7 in a virtual environment, rather than in the system globally.
Run deactivate when you need to deactivate.
You can also use virtualenv to use different Python environments.
If you type python and tab twice or so, you might see a number of Python versions available. I say this because when I type
python3.6 -V
I have
Python 3.6.9
available. My alias for python is 3.7.5
When you type python you are probably referring to an alias defined in .bashrc
I want to have multiple installs of Python: 2.1, 2.4, 2.7, 3.x
My IDE is Eclipse (Helios)/Pydev on MacOSX, which works great. I have a couple of Python codebases that are/will be running on different versions of Python. Also, I like Eclipse PyDev's crosslinking from source-code to documentation.
The standard recommendation seems to be: use virtualenv, and keep the installs totally separate from the builtin MacPython (2.6.1). Eclipse should never be pointing to the MacPython install. (Should PYTHONPATH even be set in such an environment?)
Before I get on with virtualenv, is there anything else I should know about this?
virtualenv doesn't impose any overhead, and I shouldn't be worried with occasional comments about breakage to nose, coverage etc?
I'm interested in hearing from Eclipse or Pydev users on MacOS.
Also if anyone has other tips on migrating a Python codebase from 2.1 -> 2.7.
A good way is to use macport to install the different version of pythons. It will have different versions of all packages for all versions of pythons that you want. They will be installed in /opt/local/.
So in Eclipse with PyDev for a particular project you can right click into the name of the project -> properties: There, in the left you choose PyDev - Interpreter/Grammar. And you click the link Click here to configure an interpreter not lister. You click on new on the top right of the new window. You give the name of the version of python you want to create the interpreter for, if you have already installed it with all required package through macport. And in the field Intepreter Executable you give the path: /opt/local/bin/pythonX.X .
After, in the previous option window: PyDev - Intepreter/Grammar, you can choose in the Interpreter menu the python version that you just installed.
For the shell, and the default path pointing to /usr/bin/python you must use: python_select (installed through macport) instead of playing with the env variables. Afterward you can use macport to update/install new packages, make sure everything is alway clean.
Having Home Brew already installed in your system, I recommend you pyenv. Most of the following information comes from this quick installation/use guide:
Installing pyenv + pyenv-virtualenv
Go to your terminal and install pyenv and the external libraries needed by Python:
brew update
brew install pyenv openssl readline sqlite3 xz zlib
Add pyenv init to your shell to enable shims and autocompletion running the following command:
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
Install the pyenv-virtualenv plugin so you can create virtual environments for each version of Python:
git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bash_profile
Reset your terminal to apply the changes exec "$SHELL" or just close your terminal and open a new one.
Gettings different Python versions
We can list all available versions with pyenv install --list:
pyenv install --list
Available versions:
2.7.15
3.0.1
..
3.7.2
For installing the Python version 3.7.2:
pyenv install 3.7.2
With pyenv versions you can see all the Python versions installed in your computer:
root#Host ~$ pyenv versions
* 3.7.2
Creating a virtual environment
In the previous step we have downloaded the Python 3.7.2 interpreter, we can now use it to create an isolated virtual environment. This is very useful for software development, keeping each project completely isolated from the others:
pyenv virtualenv 3.7.2 MY_VIRTUALENV_NAME
To list all your virtual environments:
pyenv virtualenvs
To activate a virtual environment:
pyenv activate MY_VIRTUALENV_NAME
To deactivate the currently active virtual environment:
pyenv deactivate
From the README text file of python
Installing multiple versions
On Unix and Mac systems if you intend
to install multiple versions of Python
using the same installation prefix
(--prefix argument to the configure
script) you must take care that your
primary python executable is not
overwritten by the installation of a
different version. All files and
directories installed using "make
altinstall" contain the major and
minor version and can thus live
side-by-side. "make install" also
creates ${prefix}/bin/python which
refers to ${prefix}/bin/pythonX.Y. If
you intend to install multiple
versions using the same prefix you
must decide which version (if any) is
your "primary" version. Install that
version using "make install". Install
all other versions using "make
altinstall".
For example, if you want to install
Python 2.5, 2.6 and 3.0 with 2.6 being
the primary version, you would execute
"make install" in your 2.6 build
directory and "make altinstall" in the
others.
Virtualenv is an option but you could use the above mentioned option instead of venv which seems much simpler.
Personally, I use conda to create multiple environments (mostly, you create a new env, activate it and install the packages you want there -- using conda itself if possible or pip if it's not available in conda). See: https://conda.io/docs/installation.html.
After you have an environment created, you just need to add a new interpreter inside PyDev which points to the interpreter in the created environment. Ssee: http://www.pydev.org/manual_101_interpreter.html.
I installed a specific version of python with pyenv. When typed pyenv version in terminal, i see 3.5.0 (set by /Users/lcherukuri/.python-version). But when I typed python3, I got python3 command not found. How to fix this? pip3 is Also not found
pyenv manages shim executables for commands like python3 and pip3. If pyenv's shims aren't available in your shell, it usually means one of two things:
pyenv isn't fully installed
or
pyenv shell features aren't active
As your pyenv command is working but the shims aren't, it most likely means the shell features aren't activated. As of writing, the correct way is to ensure the init command output is evaluated. On macOS, you can add the following to your ~/.bash_profile:
eval "$(pyenv init -)"
Older installation instructions might not include that step or just have you add pyenv's bin directory to the PATH, which is not enough. If you used pyenv-installer, this step is hinted at in a warning at the end of the installation process.
If you installed both python 2.x and python 3.x using pyenv, run the following to enable both versions to be found globally (python, python2 and python3 aliases).
Add the specific versions you are using:
pyenv global 3.8.3 2.7.18
pyenv is just a Python version manager. It may be able to see a Python 3.X installed even if python3 isn't installed in your $PATH.
You need to add python3 to your $PATH. You can see how to do that here.
By default, MacOS uses python3 to differentiate between the native pre-installed python (which is Python 2.7) and any post-installed Python 3.X distributions. The same goes for pip and pip3.
From the pyenv documentation on managing versions:
Locating the Python Installation
Once pyenv has determined which
version of Python your application has specified, it passes the
command along to the corresponding Python installation.
Each Python version is installed into its own directory under $(pyenv
root)/versions.
For example, you might have these versions installed:
$(pyenv root)/versions/2.7.8/
$(pyenv root)/versions/3.4.2/
$(pyenvroot)/versions/pypy-2.4.0/
As far as pyenv is concerned, version names
are simply the directories in $(pyenv root)/versions.
I had the python3 in path. I also executed pyenv global 2.x.x 3.x.x. But I still got the same error.
What eventually worked for me is executing this line in the project root (with whichever version replacing 3.X.X)
pyenv shell 3.X.X
Note: This sets the shell specific python version so it's not really a solution to the problem posted. Just a workaround to get python3 working.