简体   繁体   中英

Python3.7 in terminal

Sorry for dumb question, but recently I've reinstalled OS on my computer, and I'm having a bit of an issue with Linux terminal. Previously i was able to run python3.7 from terminal using "python" command, but now, instead of 3.7 - it runs 2.7, which was installed by default. Is there any way to replace 2.7 with 3.7 in "python" command, without having to type "python3.7" or "python3" (I've also installed 3.6 by accident, so it's used when executing "python3")? I'm also having the same issue with pip. When i run pip - it says that command not found, but when i type "pip3" - it runs pip3 for 3.6, and I'm only able to run 3.7's pip through "python3.7 -m pip".

You system's default version of Python is Python2.x, if you want to use Python3.x you can you one of the following methods:

  1. Create a virtual environment with python3.x. This is recommended so that when you install modules etc. it doesn't interfere with your system's python. Link to venv docs .

python3 -m venv /path/to/virtualenvironment

And use by

source /path/to/virtualenvironment/bin/activate

Instead of typing the above line you can place a function in your .bashrc :

# My functions
workon() {
        source ~/.venvs/$1/bin/activate
}

Now provide your virtual environments are saved in ~/.venvs/ , typing workon new_env will run virtual envrionment called new_env . To stop using the venv, type deactivate .

  1. Create an alias for Python to Python3:

In your .bash_aliases add the line:

alias python='python3' (If in future you DO want python2 , type python2 or \\python (the leading backslash tells bash not to use your alias.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM