Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the problem of switching versions of Python

Shulou Source: shulou.com Published: 2022-06-01 02:50:18 09月14日 Update

Most people do not understand the knowledge points of this article "how to solve the problem of switching versions of Python", so the editor summarizes the following, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to solve the problem of switching versions of Python" article.

Overview

As mac will install python2.x by default, which causes inconvenience to our python development, we often have to use the version of python3.x for testing and development, for example, open source projects have 2.x and 3.x, we have to switch back and forth to use different versions of python for testing and development, which is very inconvenient, so we have to install pyenv on the computer by switching the python version, similar to the nvm,python version in nodejs.

Other platforms

Windows

Linux: for the Ubuntu 16.04 LTS version, Python2 (2.7.12) and Python3 (3.5.2) are installed by default, and the default python version is 2.7.12.

1. Install Homebrew

Terminal execution (official website)

/ bin/bash-c "$(curl-fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"2, install pyenv through the brew command

1) enter on the command line:

# it's best to update and install again. If you haven't updated homebrew for too long, you may not be able to install brew update# and install pyenv. The latest 2.xbrew install pyenv is currently available.

2) modify the ~ / .zshrc file of the home directory

Vi ~ / .zshrc adds export PYENV_ROOT= "$HOME/.pyenv" export PATH= "$PYENV_ROOT/shims:$PATH" if command-v pyenv 1 > / dev/null 2 > & 1 at the end; then eval "$(pyenv init -)" fi

3) enter on the command line to update the environment variables

Source / .zshrc

Switching between python versions cannot be performed without execution.

3. Use pyenv to install Python3.7.5

1) check the version that can be installed (only the version number is the official version, the rest is the derivative version)

Pyenv install-list

2) install the required version

Pyenv install-v 3.7.5

3) Update the database

Pyenv rehash

4) View the installed version of the system

Pyenv versions

The * symbol indicates the version currently being used by the system

5) switch python version

Pyenv local 3.7.5python-VPython 3.7.5pyenv local systempython-VPython 2.7.16

6) current python version

Python-version

7) try to restart the terminal

4. Solve the problem that the installation speed of Python is slow with pip command, and use domestic image instead.

Vi ~ / .pip / pip.conf [global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple [install] trusted-host=mirrors.aliyun.com5, invalid version switch, try bash

# finally add vi ~ / .bashrcexport PYENV_ROOT= "$HOME/.pyenv" export PATH= "$PYENV_ROOT/shims:$PATH" if command-v pyenv 1 > / dev/null 2 > & 1; then eval "$(pyenv init -)" fisource ~ / .bashrc

Be careful

If homebrew is too slow, you can switch to domestic Source 2 or install v2ray, copy the terminal agent command, paste it to the command line, and the command line can surf the Internet scientifically.

Since the mac terminal defaults to zsh, if you are used to bash, refer to the above bash settings, it is the same.

Zsh switch version is valid. You can try bash. Granny can test it.

The above is about the content of this article on "how to solve the problem of switching versions of Python". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Tags: Version switch command content terminal update problem development test official article knowledge article system input different valid inconvenient free value Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Docker NVidia macOS Linux