简体   繁体   English

终端中的 Python3.7

[英]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.抱歉问了一个愚蠢的问题,但最近我在我的电脑上重新安装了操作系统,我在 Linux 终端上遇到了一些问题。 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.以前我可以使用“python”命令从终端运行 python3.7,但现在,而不是 3.7 - 它运行 2.7,这是默认安装的。 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")?有没有办法在“python”命令中用3.7替换2.7,而不必输入“python3.7”或“python3”(我也偶然安装了3.6,所以在执行“python3”时使用它)? I'm also having the same issue with pip.我也遇到了同样的 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".当我运行 pip 时-它说找不到该命令,但是当我键入“pip3”时-它为 3.6 运行 pip3,而我只能通过“python3.7 -m pip”运行 3.7 的 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:您系统默认的Python版本是Python2.x,如果您想使用Python3.x,您可以使用以下方法之一:

  1. Create a virtual environment with python3.x.使用 python3.x 创建虚拟环境。 This is recommended so that when you install modules etc. it doesn't interfere with your system's python.建议这样做,以便在安装模块等时不会干扰系统的 python。 Link to venv docs .链接到 venv 文档

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 :您可以在.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 .现在~/.venvs/您的虚拟环境保存在~/.venvs/ ,输入workon new_env将运行名为new_env虚拟环境。 To stop using the venv, type deactivate .要停止使用 venv,请键入deactivate

  1. Create an alias for Python to Python3:创建 Python 到 Python3 的别名:

In your .bash_aliases add the line:在您的.bash_aliases添加以下行:

alias python='python3' (If in future you DO want python2 , type python2 or \\python (the leading backslash tells bash not to use your alias. alias python='python3' (如果以后你确实想要python2 ,输入python2\\python (前导反斜杠告诉bash不要使用你的别名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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