Home > Software engineering >  How to do I update my python environment, my python setup is a mess and I installed it in many diffe
How to do I update my python environment, my python setup is a mess and I installed it in many diffe

Time:01-14

When I first installed Python, I think I did it using the website. But soon after, I found that there were many ways to install python, I started hearing things like pyenv, pip (am familiar with this), anaconda, etc., and I used Homebrew the second time.

There were lots of terminal work to do that I really didn't understand. Now I downloaded python again into my computer and when I use python on the terminal, it does not reflect the updated version. enter image description here

but I downloaded the latest Python 3.10 version. I don't know how to fix this.

P.S. I would be open to completely removing Python from my system, and even Homebrew and start from scratch, as I only really got Homebrew for Python.

CodePudding user response:

Of course, relevant xkcd.

I'm sure you'll get many different answers, but after struggling for months with environment management when I first started coding, I finally ended up finding that pyenv was the best option for me.

You can use Homebrew to install pyenv and virtualenv.

Pyenv installation instructions for MacOS: https://github.com/pyenv/pyenv#homebrew-in-macos Virtualenv installation instructions for MacOS: https://github.com/pyenv/pyenv-virtualenv

Of course you'll have to make sure to carefully follow the installation instructions for whatever terminal shell you use, zsh, bash, etc. The linked GitHub installation instructions explain this.

After you've successfully installed the two, you can use pyenv to install specific base versions of Python, e.g., pyenv install 3.8.8.

You can then use virtualenv to create specific closed environments for each Python version. I use this frequently for building/testing with packages that aren't compatible with one another.

You can activate the environments with commands as so: pyenv virtualenv 3.8.8 app_build, and to create another one, pyenv virtualenv 3.8.8 app_testing.

Then you can activate each environment for usage in your terminal or IDE by using pyenv activate app_build, etc. After activation, you can install packages, and they will only be installed in the activated environment.

Pyenv integrates very nicely with Visual Studio Code as well. I can speak more to that if you're interested.

I used anaconda for a while, but it wasn't well suited for app development/PyQt5, so I abandoned it. I personally like much pyenv much more, as I feel more in control with it.

CodePudding user response:

"python3" is the python of the system. Same as "/usr/bin/python3":

/usr/bin/python3
Python 3.8.9 (default, Oct 26 2021, 07:25:54) 
[Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

You want to use "/usr/local/bin/python3":

/usr/local/bin/python3
Python 3.9.9 (main, Nov 21 2021, 03:23:44) 
[Clang 13.0.0 (clang-1300.0.29.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

CodePudding user response:

  1. Choose the python version you would like to install from:

    pyenv install --list

I have chosen 3.10.1.

  1. Install python:

    pyenv install 3.10.1

  2. Switch to it in the current shell:

    pyenv shell 3.10.1

  3. Create virtual environment for the project:

    python -m venv <new_env_directory>

  4. Activate the newly created environment:

    <new_env_directory>/bin/activate

  •  Tags:  
  • Related