为什么从Terminal和Atom调用时python的版本不同 [英] Why python's versions differ when called from Terminal and from Atom

查看:96
本文介绍了为什么从Terminal和Atom调用时python的版本不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从终端和Atom运行Python时,将调用不同版本的python.结果,有些程序包可以在终端上正常调用但不能从Atom调用.详细信息如下:

terminal调用python时,我得到版本3.7.1

$ python
Python 3.7.1 | packaged by conda-forge | (default, Nov 13 2018, 09:50:42) 
[Clang 9.0.0 (clang-900.0.37)] :: Anaconda custom (64-bit) on darwin

(如果我键入python3,我会得到完全相同的结果)

现在,当从Atom中查看python版本时,我使用的是3.6.5版本

>>> import sys
>>> print(sys.version
3.6.5 | packaged by conda-forge | (default, Apr  6 2018, 13:44:09) 
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)]

怎么来的?如何使Atom获得与终端所获得版本相同的版本?

尝试失败

使用命令import sys依次查看PATHprint(sys.path),这是我得到的:

来自Atom:

['', '/anaconda3/lib/python36.zip', '/anaconda3/lib/python3.6', 
'/anaconda3/lib/python3.6/lib-dynload', '/anaconda3/lib/python3.6/site-packages',
'/anaconda3/lib/python3.6/site-packages/aeosa', '/anaconda3/lib/python3.6/site-packages/cycler-0.10.0-py3.6.egg', 
'/anaconda3/lib/python3.6/site-packages/IPython/extensions', '/Users/mymac/.ipython']

从终端:

['', '/Users/mymac/anaconda3/lib/python37.zip', 
'/Users/mymac/anaconda3/lib/python3.7', 
'/Users/mymac/anaconda3/lib/python3.7/lib-dynload', 
'/Users/mymac/anaconda3/lib/python3.7/site-packages', 
'/Users/mymac/anaconda3/lib/python3.7/site-packages/aeosa']

给出以下命令和输出,看来Atom正在使用pyenv的Python版本:

$ pyenv versions
* system (set by /Users/mymac/.pyenv/version)
  3.6.5

根据 pyenv的github页面.我输入了pyenv virtualenv 3.7.1.然后,当输入$ pyenv versions时,列表中至少有3.7.1版本:

* system (set by /Users/mymac/.pyenv/version)
  3.6.5
  3.7.1

但这并不能解决Atom

的问题

然后,我访问了 conda页面关于管理环境,并运行以下命令:

$ conda create --name myenv

它运行没问题,但问题仍然存在

然后我尝试更具体地运行$ conda create -n myenv python=3.7.1,但得到了:CondaValueError: prefix already exists: /Users/mymac/anaconda3/envs/myenv

如@jmh所指示,然后我尝试:$ source activate 3.7.1并得到以下提示:(3.7.1) user:~ user$

我可以成功选择正确的虚拟环境.但是打开Atom后问题仍然存在

解决方案

好,我实际上必须更改jupyter内核的.json文件中的Path.首先,我检查了jupyter内核列表:

$ jupyter kernelspec list
julia-0.6    /Users/mymac/Library/Jupyter/kernels/julia-0.6
julia-1.0    /Users/mymac/Library/Jupyter/kernels/julia-1.0
python3      /Users/mymac/Library/Jupyter/kernels/python3

然后我cd上面的python路径,我在里面找到文件kernel.json并打开了它:

{
 "argv": [
  "/path/to/python",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "Python 3",
 "language": "python"
}

在该文件中,然后通过在终端机中键入以下内容时得到的python路径更改行/path/to/python:

$ which python
/Users/mymac/anaconda3/bin/python

重新启动Atom,终于成功了!

jupyter的github页面的提示也很有帮助!

When I run Python from the terminal and from Atom, different versions of python are called. As a consequence, there are packages that I can call without problems from the terminal but that I cannot call from Atom. Here are the details:

When calling python from the terminal, I get version 3.7.1

$ python
Python 3.7.1 | packaged by conda-forge | (default, Nov 13 2018, 09:50:42) 
[Clang 9.0.0 (clang-900.0.37)] :: Anaconda custom (64-bit) on darwin

(I get exactly the same if I type python3instead)

Now when looking at the python version from Atom i get version 3.6.5 with

>>> import sys
>>> print(sys.version
3.6.5 | packaged by conda-forge | (default, Apr  6 2018, 13:44:09) 
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)]

how come? how can I make Atom get the same version as the version the Terminal is getting?

Failed attempts

Looking at the PATH with the command import sys and then print(sys.path) here is what I get:

From Atom:

['', '/anaconda3/lib/python36.zip', '/anaconda3/lib/python3.6', 
'/anaconda3/lib/python3.6/lib-dynload', '/anaconda3/lib/python3.6/site-packages',
'/anaconda3/lib/python3.6/site-packages/aeosa', '/anaconda3/lib/python3.6/site-packages/cycler-0.10.0-py3.6.egg', 
'/anaconda3/lib/python3.6/site-packages/IPython/extensions', '/Users/mymac/.ipython']

From the Terminal:

['', '/Users/mymac/anaconda3/lib/python37.zip', 
'/Users/mymac/anaconda3/lib/python3.7', 
'/Users/mymac/anaconda3/lib/python3.7/lib-dynload', 
'/Users/mymac/anaconda3/lib/python3.7/site-packages', 
'/Users/mymac/anaconda3/lib/python3.7/site-packages/aeosa']

It seems Atom is using the version of Python of pyenv given the following command and output:

$ pyenv versions
* system (set by /Users/mymac/.pyenv/version)
  3.6.5

according to the github page of pyenv. I entered pyenv virtualenv 3.7.1. Then when entering $ pyenv versions at least the version 3.7.1 was in the list:

* system (set by /Users/mymac/.pyenv/version)
  3.6.5
  3.7.1

But that didn't solve the problem with Atom

I then visited the conda page aboute managing environment and ran the following commands:

$ conda create --name myenv

It ran without problem, but the problem persists

I then tried to run more specifically $ conda create -n myenv python=3.7.1 but got: CondaValueError: prefix already exists: /Users/mymac/anaconda3/envs/myenv

As pointed by @jmh Then I tried: $ source activate 3.7.1 and got the following prompt: (3.7.1) user:~ user$

I could successfully choose the correct the correct virtualenv. But then upon opening Atom the problem persisted

解决方案

Ok, I had actually to change the Path in the .json file of the jupyter kernel. First I checked the list of jupyter kernel:

$ jupyter kernelspec list
julia-0.6    /Users/mymac/Library/Jupyter/kernels/julia-0.6
julia-1.0    /Users/mymac/Library/Jupyter/kernels/julia-1.0
python3      /Users/mymac/Library/Jupyter/kernels/python3

I then cd the above python path and, I found the file kernel.json inside and opened it:

{
 "argv": [
  "/path/to/python",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "Python 3",
 "language": "python"
}

In that file, I then changed the line /path/to/python by the python path I got when typing the following in Terminal:

$ which python
/Users/mymac/anaconda3/bin/python

Relaunched Atom and it finally worked!

The hints of the github page of jupyter helped also a lot!

这篇关于为什么从Terminal和Atom调用时python的版本不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆