How to switch python3 versions on MacOs [duplicate] - python

This question already has answers here:
How do I use installed packages in PyCharm?
(14 answers)
Closed last year.
I am using python version 3.10.2 but I want to downgrade to 3.9 because it is not compatible with a library in my project. How can I do this with terminal command?
So when I type python3 --version my version is 3.10.2 I want to downgrade it. IDE is pycharm

Install pyenv with Homebrew on macOS:
brew update
brew install pyenv
Define your vitual env variables:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
source ~/.bash_profile
Restart your shell so the path changes take effect
exec "$SHELL"
Check the available python versions:
pyenv install --list
Install the required python version:
pyenv install 3.9
Set it as your global version:
pyenv global 3.9

Related

How do I get pyenv to display the executable path for an installed version?

Install a python version using:
$ pyenv install 3.8.9
Installed Python-3.8.9 to /Users/robino/.pyenv/versions/3.8.9
List the python versions now available:
$ pyenv versions
* system
3.8.2
3.8.9
A week goes by and I forget where it is installed. Now suppose I want to get the executable path for 3.8.9 version. The following do not work:
$ pyenv which 3.8.9
pyenv: 3.8.9: command not found
$ pyenv which python 3.8.9
(gives path to system python)
$ pyenv which python-3.8.9
pyenv: python-3.8.9: command not found
$ pyenv which Python-3.8.9
pyenv: Python-3.8.9: command not found
A workaround I found was to set the python version, check, then set it back to system:
$ pyenv local 3.8.9
$ pyenv which python
/Users/robino/.pyenv/versions/3.8.9/bin/python
$ pyenv local --unset
However this is a suboptimal solution as it required that no local is previous set.
What is the correct command to print out the python executable path for a currently not used version, using pyenv?
By default, pyenv executable can be found at $(pyenv root)/versions/{VERSION}/bin/python. I am not aware of a command displaying all/any executables other than pyenv which python.
If you'd like to get the path via commands though, another option would be to make a temporary subdirectory and set the local pyenv interpreter there:
$ mkdir tmp; cd tmp
$ pyenv local 3.8.9
$ pyenv which python
/Users/robino/.pyenv/versions/3.8.9/bin/python
$ cd ..; rm -r tmp
Since deeper directories take priority with local pyenv versions, a parent directory wouldn't interfere in this case.
Yet another option would be to temporarily set the global pyenv version, as this does not have the requirement of no local pyenv version being set. I wouldn't like this though as I'd probably forget to set it back to its original value ;)
I simplified it a little, and the command runs in a subcommand, should be immune to side effect.
echo $(pyenv shell 3.9.15; pyenv which python)
# /Users/tomy0000000/.pyenv/versions/3.9.15/bin/python

How to downgrade the python on macbook from 3.8 to 3.5

I've tried several ways to downgrade the python3, including using pyenv, but it has the build failed error. I think it's probably because my python3 is in my usr/bin folder and I can not delete it. For the course requirement, I really need to downgrade python to 3.5. Is there any way to do so?
I recommend using Anaconda to install the specific python version. like it:
conda create -n py35 python=3.5
First of all, install brew if is not already installed. Then add it to user path by:
export PATH=/opt/homebrew/bin:$PATH
Once done Restart your shell:
exec zsh -l
Now Check Python is located where:
which python
Next, Go through following steps:
Check if you have pyenv already installed on mac OS.
pyenv --version -- Optional since you already stated that pyenv is present at your machine.
List all available version for python:
pyenv install --list
Install desired python version:
pyenv install 3.5
Set python 3.5 version globally:
pyenv global 3.5
Check again if python is located whether in user's directory:
which python
it should be pointing to your /Users/usrone/.pyenv/shims/python
if not then set it again : pyenv global 3.5
Update your .zshrc
if command -v pyenv 1>/dev/null 2>&1; then eval "$(pyenv init -)" fi PATH=$(pyenv root)/shims:$PATH
Restart zsh terminal:
exec zsh -l
You can also read excerpts from Downgrade Python version SO answer

Install Python 3.10.5 using pyenv

I want to install Python 3.10.5 (or at least Python >= 3.7.). I followed these instructions:
# Step 1. Install pyenv
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bashrc
# Step 2. Install missing headers for all the Python modules to be built
sudo zypper install readline-devel sqlite3-devel libbz2-devel
# Step 3. Install the desired Python version
pyenv install 3.10.5
until I got to pyenv install 3.10.5:
bscuser#localhost:~> pyenv install 3.10.5
/home/bscuser/.pyenv/plugins/python-build/share/python-build/3.7.13: line 1: prefer_openssl11: command not found
BUILD FAILED (openSUSE 15.3 using python-build 20180424)
I feel like it should be related to this file.
How do I solve this?
Yes, you can. Use pyenv update first then running pyenv install -list you should be able to see versions 3.10.4, 3.10.5, 3.10.6, 3.11.0, even 3.11.1 now
I don't think that's possible.
Running pyenv install -list will give you a list of all possible python versions that can be installed, and it will only go as far as 3.10.4 as of this writing.
here is similar case:
OS: macOS, previously installed old version pyenv 1.2.26
-> pyenv install -l see the latest python version is 3.10-dev, not expected 3.10.x
so need upgrade pyenv to latest version, here pyenv 2.3.3:
on macOS: brew upgrade pyenv
you can refer official doc to install/upgrade to latest pyenv
then can list to latest python 3.10.5+:
> pyenv install --list | grep 3.10
...
3.10.0
3.10-dev
3.10.1
3.10.2
3.10.3
3.10.4
3.10.5
3.10.6
mambaforge-4.10.3-10
miniconda-3.10.1
...
then can use pyenv install 3.10.6 normally:
pyenv install 3.10.6
so, here key point is:
upgrade pyenv to latest version (here pyenv 2.3.3), then pyenv install --list can list/see latest python and install it pyenv install 3.10.6

Make the 'python' phrase refer to Python 3 on Linux?

So whenever I run python -v it says I'm using Python 2, why is python reserved for Python 2 on Ubuntu, and is there a way to make python reserve python 3? The latest version.
For example, Id prefer to write
`python /files/app.py`
To execute my files than write
`python3 /files/app.py`
It seems messy to have to add the 3, you don't on Windows, so why is Ubuntu different?
You can use an alias to do this. Add the following line to your .bashrc:
alias python=python3
Don't forget to reopen your terminal, or do source ~/.bashrc for the changes to take effect.
Duplicate of this post.
Ubuntu does this because you probably have two versions of Python installed, a system variant and a 3.x variant you are using. You can try python --version and python3 --version to see. If they return different things, you have two installations.
For example:
$ python --version
Python 2.7.6
$ python3 --version
Python 3.4.3
$ alias python=python3
$ python --version
Python 3.4.3
To circumvent the alias use the command built-in command:
$ command python --version
Python 2.7.6
Another way to circumvent the alias is to use \ before the command.
$ \python --version
Python 2.7.6
To disable the alias in the current shell use the unalias built-in command:
$ unalias python
$ python --version
Python 2.7.6
As already stated here, you have system python and user python and one option is to create aliases for python and pip on a user level ~/.bashrc
Another option is to create a virtual environment for each project you work on. The advantage of this is that you only install modules relevant to each project.
Create a virtual environment
mkdir someproject
cd someproject/
python3 -m venv venv
Activate environment
source venv/bin/activate
Check python and pip version
$ pip -V
pip 9.0.1 from ~/someproject/venv/lib/python3.6/site-packages (python 3.6)
$ python -V
Python 3.6.7
Deactivate environment
deactivate
Python, pip and any other modules you decide to install is installed in the venv folder instead of system wide.
Only gotcha is to remember to activate your environment.

pyenv: pip: command not found

I'm trying to get Python 2.7 to play nicely with pyenv and virtualenv on OSX El Capitan. I've installed pyenv with Homebrew, then Python 2.7.11 with pyenv install 2.7.11. This describes the setup:
$ which python
/usr/local/bin/python
$ which virtualenv
/usr/local/bin/virtualenv
$ head -1 /usr/local/bin/virtualenv
#!/usr/local/bin/python
$ which pip
/Users/username/bin/pip
$ head -1 /Users/robinedwards/bin/pip
#!/usr/local/bin/python
$ pyenv install --list | grep 2.7.11
2.7.11
.bashrc contains lines:
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
Now when I try to create a new virtualenv project:
$ pyenv virtualenv 2.7.11 newproject
pyenv: pip: command not found
Any idea what I'm doing wrong?
I'm not sure if that solution matches the problem, but after installing pyenv, pyenv-virtualenv and selecting a python version, I had to run pip that way:
$ pyenv exec pip install
I had the same error message. The problem was due to a failed installation of a python version, so pip wasn't found for this version. In fact, even python wasn't found.
example:
pyenv install 3.7.2 # this failed, but I did not realize it failed at first
pyenv versions | grep 3.7.2
3.7.2
pyenv local 3.7.2
python --version
pyenv: python: command not found
So problem was not with pip itself, but a broken installation of a python version. Just make sure you succeed when you install a python version with pyenv.
You need to install pip separately if you didn't install pyenv from the binaries provided by python.org
After installing the wanted python version, download and install pip :
wget https://bootstrap.pypa.io/get-pip.py
(sudo) python ./get-pip.py​
rm get-pip.py
Ref: https://pip.pypa.io/en/stable/installing/
After installing a python version with pyenv with
pyenv install 3.7.13
I first had to run:
pyenv global 3.7.13
and now pip is working fine.
I had a bad configuration using zsh and pyenv. I solved it by removing what I had in ~/.zshrc on pyenv, removing the environment and python version (pyenv uninstall 3.9.0 in my case) and then running the following:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zprofile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zprofile
echo 'eval "$(pyenv init --path)"' >> ~/.zprofile
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.profile
echo 'eval "$(pyenv init --path)"' >> ~/.profile
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
This comes directly from the provided ~/.pyenv/README so I guess it should have been obvious. After reinstalling the python version (pyenv install 3.9.0) and creating the environment again (pyenv virtualenv 3.9.0 my-env) pip worked as expected when activating the environment via pyenv activate my-env

Categories

Resources