简体   繁体   English

如何在Ubuntu16.04中管理多个python?

[英]How can I manage mutiple python in Ubuntu16.04?

In my Ubuntu16.04, there are python 2 and python 3 default. 在我的Ubuntu16.04中,默认有python 2和python 3。 In addition, i have installed anaconda too. 另外,我也安装了anaconda。 I am sucked by the 'python' cmd. 我被'python'cmd吸了。 Every time i use pip or pip3 install, I don't know where the package install, python2 or python 3? 每次我使用pip或pip3安装,我都不知道软件包安装在哪里,python2或python 3? And I use conda install to install anaconda package. 我使用conda install来安装anaconda包。 I also use anaconda env to manage different virtual env. 我还使用anaconda env来管理不同的虚拟环境。 But I think it mix with my local Python 2 and 3. 但我认为它与我的本地Python 2和3混合。

For example, in directory /usr/bin, I found many soft links like this: 例如,在目录/ usr / bin中,我发现了许多像这样的软链接: 在此输入图像描述

When i try 'python' cmd, it just confuse me! 当我尝试'python'cmd时,它只会让我困惑! 在此输入图像描述

Why python3m are local, shouldn't it be anaconda? 为什么python3m是本地的,不应该是anaconda吗? Why python3 are anaconda, shouldn't it be local? 为什么python3是anaconda,不应该是本地的吗? Then I found that if I use ./python2 or ./python3, I found it is correct now! 然后我发现如果我使用./python2或./python3,我发现它现在是正确的! 在此输入图像描述

So I know it is caused by environment variables. 所以我知道它是由环境变量引起的。 I echo $PATH, Found it like this: /home/kinny/.pyenv/shims:/home/kinny/.pyenv/bin:/home/kinny/anaconda3/bin:/home/kinny/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/opt/ant/bin:/snap/bin:/opt/maven/bin:/usr/lib/jvm/java-8-oracle/bin 我回复$ PATH,发现它是这样的:/home/kinny/.pyenv/shims:/home/kinny/.pyenv/bin:/home/kinny/anaconda3/bin:/home/kinny/bin:/usr/local / sbin目录:在/ usr / local / bin目录:/ usr / sbin目录:在/ usr / bin中:/ sbin目录:/ bin中:在/ usr /游戏:在/ usr /本地/游戏中:/ opt /蚂蚁/斌:/卡/箱:的/ opt /行家/ bin中:/ usr / lib中/ JVM / JAVA -8-预言/ bin中

I have used update-alternative --config python to configure default python, but it doesn't work! 我使用update-alternative --config python来配置默认的python,但它不起作用! It sames mixed with each others. 它同样相互混合。 在此输入图像描述

Now I just want to install tensorflow 0.11 in local python3, because in anaconda it is 0.10 version by default. 现在我只想在本地python3中安装tensorflow 0.11,因为在anaconda中它默认为0.10版本。 So how can I change this. 那么我怎么能改变这个呢。 I just want to use python python3 and python3m represents python2.7 python3.5 and anaconda python respectively, How can I do that! 我只想使用python python3和python3m分别代表python2.7 python3.5和anaconda python,我该怎么办呢! use pip and pip3 for local python2 and python3 respectively! 分别使用pip和pip3表示本地python2和python3!

I ran into a similiar problem when setting up PyCharm Edu to work with Anaconda. 在设置PyCharm Edu与Anaconda合作时遇到了类似的问题。 I found that I had several versions of Python installed and it was very hard to keep track of which version the IDE was referencing. 我发现我安装了几个版本的Python,很难跟踪IDE引用的版本。 My CS professor gave me the advice of simply removing the versions of Python I didn't frequent. 我的CS教授给了我一个建议,就是简单地删除我不经常使用的Python版本。 I now just have Anaconda installed; 我现在只安装了Anaconda; and use the Anaconda Prompt as my Python console. 并使用Anaconda Prompt作为我的Python控制台。 I also rely on PyCharm's IPython for the developer console. 我还依赖PyCharm的IPython作为开发人员控制台。 However, if you still want differing versions of Python installed (say your doing QA testing for older devices); 但是,如果您仍然希望安装不同版本的Python(比如您为旧设备进行QA测试); there is the really helpful command: which python . 有一个非常有用的命令: which python When entered into the python console or Anaconda Prompt: which python will display the directory associated with the currently executing Python Shell. 当进入python控制台或Anaconda Prompt时: which python将显示与当前正在执行的Python Shell相关联的目录。 This enables you to better keep track of to what particular python.exe the current window is referring to. 这使您可以更好地跟踪当前窗口所指的特定python.exe。

Follow up to the comments mentioning using virtualenv and virtualenvwrapper. 跟进使用virtualenv和virtualenvwrapper提到的评论。

Here are the official docs and a good blog post to follow for getting started using virtualenv's is here: 以下是官方文档和一篇关于开始使用virtualenv的好文章:

https://virtualenv.pypa.io/en/stable/installation/ https://virtualenv.pypa.io/en/stable/installation/

http://virtualenvwrapper.readthedocs.io/en/latest/install.html http://virtualenvwrapper.readthedocs.io/en/latest/install.html

http://exponential.io/blog/2015/02/10/install-virtualenv-and-virtualenvwrapper-on-ubuntu/ http://exponential.io/blog/2015/02/10/install-virtualenv-and-virtualenvwrapper-on-ubuntu/

Also, once you are setup you can create virtualenv's specifying which python installation you want to use. 此外,一旦你进行了设置,你就可以创建virtualenv来指定你想要使用哪个python安装。

which python3

returns 回报

/usr/bin/python3

Then create a virtualenv with that python path. 然后使用该python路径创建virtualenv。 Where example_env is the name of the virtualenv. 其中example_env是virtualenv的名称。

mkvirtualenv -p /usr/bin/python3 example_env

Then activate the virtualenv using virtualenvwrapper. 然后使用virtualenvwrapper激活virtualenv。

workon example_env

Finally, install tensorflow and other dependencies with pip. 最后,使用pip安装tensorflow和其他依赖项。

pip install tensorflow

the which command is very useful for finding the path to the executable that is first in your path. which命令对于查找路径中第一个可执行文件的路径非常有用。 Zsh also has the where command, which will show you all instances of the given executable that show up in your path. Zsh还有where命令,它将显示路径中显示的给定可执行文件的所有实例。 For managing different python versions, you have a lot of options. 为了管理不同的python版本,你有很多选择。 The easiest for most people tends to be anaconda, using conda environments. 对于大多数人来说,最容易使用conda环境的人是anaconda。 The installer will ask you to add some stuff to your .bashrc file, which will then make anaconda's binaries come first in your path. 安装程序会要求您在.bashrc文件中添加一些内容,然后将anaconda的二进制文件放在您的路径中。 Anything else you run after the .bashrc gets sourced after that, will then use that first, including PyCharm. .bashrc获得源代码之后运行的任何其他内容将首先使用它,包括PyCharm。 For graphical desktop apps to pick up the change, you may need to log out and back in again. 要使图形桌面应用程序获取更改,您可能需要注销并重新登录。 If you only need one version each of python 2 and python 3, you can just use the ones available via apt . 如果你只需要python 2和python 3各一个版本,你可以使用apt Depending on your Ubuntu version, Python 2 is definitely installed by default as it is used by many system utilities, including apt itself. 根据您的Ubuntu版本,Python 2默认安装,因为它被许多系统实用程序使用,包括apt本身。 Some newer versions may also install python 3 by default, but I do not remember for sure. 一些较新的版本也可能默认安装python 3,但我不记得了。 Another option is to install the versions of python you need in an alternate location, such as /opt/python/<version> and then using environment-modules (installed via apt install environment-modules ) or Lmod to control which versions are being used, but that may or may not be easy/convenient to use with a desktop application such as PyCharm. 另一个选择是在备用位置安装所需的python版本,例如/opt/python/<version> ,然后使用environment-modules (通过apt install environment-modules )或Lmod来控制正在使用的版本,但这可能是也可能不容易/方便地与桌面应用程序如PyCharm一起使用。

for TensorFlow, 1.11 is available in anaconda, but I don't remember if it's in the default channel or not. 对于TensorFlow,在anaconda中可以使用1.11,但我不记得它是否在默认频道中。

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

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