在 Cygwin 中为 Anaconda 永久设置 Python 路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24255407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Permanently set Python path for Anaconda within Cygwin
提问by thomastodon
I'm trying to install Anaconda on my Windows 7 machine. I often use cygwin to for my command-line work, and I would like to manage Anaconda from there. I've worked through the graphic installer without any issues, and checked necessary boxes to reset my default path to this install of python. I go ahead to check where python is and initially I get this...
我正在尝试在我的 Windows 7 机器上安装 Anaconda。我经常将 cygwin 用于我的命令行工作,我想从那里管理 Anaconda。我已经通过图形安装程序没有任何问题,并检查了必要的框以重置我安装 python 的默认路径。我继续检查python在哪里,最初我得到了这个......
$ which python
/usr/bin/python
From here python works fine...
从这里 python 工作正常......
$ python
Python 2.7.5 (default, Oct 2 2013, 22:34:09)
[GCC 4.8.1] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
But I'm trying to work from anaconda, so I should just need to redefine my path...
但是我正在尝试从 anaconda 工作,所以我只需要重新定义我的路径......
$ export PATH=/cygdrive/c/anaconda:$PATH
$ which python
/cygdrive/c/anaconda/python
And now I should be good to go, but when I try and step into python, it just hangs
现在我应该可以走了,但是当我尝试进入 python 时,它就挂了
$ python
Any idea why this might be happening? verbose return, below...
知道为什么会发生这种情况吗?详细返回,下面...
$ python -v
# installing zipimport hook
import zipimport # builtin
# installed zipimport hook
# C:\anaconda\lib\site.pyc matches C:\anaconda\lib\site.py
import site # precompiled from C:\anaconda\lib\site.pyc
# C:\anaconda\lib\os.pyc matches C:\anaconda\lib\os.py
import os # precompiled from C:\anaconda\lib\os.pyc
import errno # builtin
import nt # builtin
# C:\anaconda\lib\ntpath.pyc matches C:\anaconda\lib\ntpath.py
import ntpath # precompiled from C:\anaconda\lib\ntpath.pyc
# C:\anaconda\lib\stat.pyc matches C:\anaconda\lib\stat.py
import stat # precompiled from C:\anaconda\lib\stat.pyc
# C:\anaconda\lib\genericpath.pyc matches C:\anaconda\lib\genericpath.py
import genericpath # precompiled from C:\anaconda\lib\genericpath.pyc
# C:\anaconda\lib\warnings.pyc matches C:\anaconda\lib\warnings.py
import warnings # precompiled from C:\anaconda\lib\warnings.pyc
# C:\anaconda\lib\linecache.pyc matches C:\anaconda\lib\linecache.py
import linecache # precompiled from C:\anaconda\lib\linecache.pyc
# C:\anaconda\lib\types.pyc matches C:\anaconda\lib\types.py
import types # precompiled from C:\anaconda\lib\types.pyc
# C:\anaconda\lib\UserDict.pyc matches C:\anaconda\lib\UserDict.py
import UserDict # precompiled from C:\anaconda\lib\UserDict.pyc
# C:\anaconda\lib\_abcoll.pyc matches C:\anaconda\lib\_abcoll.py
import _abcoll # precompiled from C:\anaconda\lib\_abcoll.pyc
# C:\anaconda\lib\abc.pyc matches C:\anaconda\lib\abc.py
import abc # precompiled from C:\anaconda\lib\abc.pyc
# C:\anaconda\lib\_weakrefset.pyc matches C:\anaconda\lib\_weakrefset.py
import _weakrefset # precompiled from C:\anaconda\lib\_weakrefset.pyc
import _weakref # builtin
# C:\anaconda\lib\copy_reg.pyc matches C:\anaconda\lib\copy_reg.py
import copy_reg # precompiled from C:\anaconda\lib\copy_reg.pyc
# C:\anaconda\lib\traceback.pyc matches C:\anaconda\lib\traceback.py
import traceback # precompiled from C:\anaconda\lib\traceback.pyc
# C:\anaconda\lib\sysconfig.pyc matches C:\anaconda\lib\sysconfig.py
import sysconfig # precompiled from C:\anaconda\lib\sysconfig.pyc
# C:\anaconda\lib\re.pyc matches C:\anaconda\lib\re.py
import re # precompiled from C:\anaconda\lib\re.pyc
# C:\anaconda\lib\sre_compile.pyc matches C:\anaconda\lib\sre_compile.py
import sre_compile # precompiled from C:\anaconda\lib\sre_compile.pyc
import _sre # builtin
# C:\anaconda\lib\sre_parse.pyc matches C:\anaconda\lib\sre_parse.py
import sre_parse # precompiled from C:\anaconda\lib\sre_parse.pyc
# C:\anaconda\lib\sre_constants.pyc matches C:\anaconda\lib\sre_constants.py
import sre_constants # precompiled from C:\anaconda\lib\sre_constants.pyc
# C:\anaconda\lib\locale.pyc matches C:\anaconda\lib\locale.py
import locale # precompiled from C:\anaconda\lib\locale.pyc
import encodings # directory C:\anaconda\lib\encodings
# C:\anaconda\lib\encodings\__init__.pyc matches C:\anaconda\lib\encodings\__init__.py
import encodings # precompiled from C:\anaconda\lib\encodings\__init__.pyc
# C:\anaconda\lib\codecs.pyc matches C:\anaconda\lib\codecs.py
import codecs # precompiled from C:\anaconda\lib\codecs.pyc
import _codecs # builtin
# C:\anaconda\lib\encodings\aliases.pyc matches C:\anaconda\lib\encodings\aliases.py
import encodings.aliases # precompiled from C:\anaconda\lib\encodings\aliases.pyc
import operator # builtin
# C:\anaconda\lib\functools.pyc matches C:\anaconda\lib\functools.py
import functools # precompiled from C:\anaconda\lib\functools.pyc
import _functools # builtin
import _locale # builtin
# C:\anaconda\lib\encodings\cp1252.pyc matches C:\anaconda\lib\encodings\cp1252.py
import encodings.cp1252 # precompiled from C:\anaconda\lib\encodings\cp1252.pyc
# zipimport: found 13 names in C:\anaconda\lib\site-packages\runipy-0.1.0-py2.7.egg
# zipimport: found 144 names in C:\anaconda\lib\site-packages\setuptools-3.6-py2.7.egg
Python 2.7.7 |Anaconda 2.0.1 (64-bit)| (default, Jun 11 2014, 10:40:02) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and htt
Another (possibly related) issue I'm having is needing to reset the path every time I close/open cygwin. I've entered the following text into .bashrc and .profile to try and set the path permanently:
我遇到的另一个(可能相关)问题是每次关闭/打开 cygwin 时都需要重置路径。我已将以下文本输入 .bashrc 和 .profile 以尝试永久设置路径:
# Set path to python from anaconda install
export PATH=/cygdrive/c/anaconda:$PATH
After opening and closing cygwin, I return to:
打开和关闭 cygwin 后,我回到:
$ which python
/usr/bin/python
Could this be related to setting certain system environment variables?
这可能与设置某些系统环境变量有关吗?
回答by Martin Konecny
Regarding setting your path permanently, edit the .profile in your home directory(it's a hidden file).
关于永久设置路径,请编辑主目录中的 .profile(它是一个隐藏文件)。
.profile (other names are also valid, see the bash man page) contains bash commands. It is executed when bash is started as login shell.
.profile(其他名称也有效,请参阅 bash 手册页)包含 bash 命令。它在 bash 作为登录 shell 启动时执行。
At the bottom of this file, put your set path command:
在此文件的底部,放置您的设置路径命令:
export PATH=/cygdrive/c/anaconda:$PATH
As for why python is hanging, please provide more information - for example what's the output of python -v
?
至于为什么python挂了,请提供更多信息-例如输出是python -v
什么?
回答by dirkjot
Path
小路
Regarding your path issue, see Installing anaconda to use with windowson how to fix that. Alternatively, you can just run /cygdrive/c/Anaconda/python.exe
to start the Anaconda/windows python and leave the standard python
pointing to the cygwin version.
关于您的路径问题,请参阅安装 anaconda 以与 Windows 一起使用以了解如何解决该问题。或者,您可以运行/cygdrive/c/Anaconda/python.exe
以启动 Anaconda/windows python 并保留python
指向 cygwin 版本的标准。
Note that few of the suggestions given on the internet also include the necessary change to PYTHONPATH
: if you use the anaconda/windows python, you also want the anaconda python libraries. Binary modules like numpy
will surely crash or misbehave if you mix things up. The easiest way to check this is to install a module in cygwin that you do not need in Conda and test whether you can import that from anaconda python. If you can, your path is wrong.
请注意,互联网上给出的建议很少也包括对以下内容的必要更改PYTHONPATH
:如果您使用 anaconda/windows python,您还需要 anaconda python 库。numpy
如果你把东西混在一起,像二进制模块肯定会崩溃或行为不端。检查这个最简单的方法是在 cygwin 中安装一个你在 Conda 中不需要的模块,并测试你是否可以从 anaconda python 导入它。如果可以,那么你的路径是错误的。
Anaconda python will automatically add c:\Anconda
to your PYTHONPATH, but if you have it set to serve CYGWIN python libraries, those will be found before the Anaconda libs. Try
Anaconda python 将自动添加c:\Anconda
到您的 PYTHONPATH,但如果您将其设置为提供 CYGWIN python 库,那么这些将在 Anaconda 库之前找到。尝试
import sys
sys.path
Terminal
终端
However, this is also a terminal issue: Anaconda (Windows) python expects a windows shell and a modern cygwin shell is like an xterm
, so very different. Diagnose this by running python --help
, if that works but plain python
hangs, the interactive prompt is the problem. This means you can run python programs, you just cannot interact with them.
然而,这也是一个终端问题:Anaconda (Windows) python 需要一个 windows shell,而现代的 cygwin shell 就像一个xterm
,所以非常不同。通过运行诊断此问题python --help
,如果该方法有效但正常python
挂起,则交互提示是问题所在。这意味着您可以运行 python 程序,但不能与它们交互。
A way around this is to use the cygwin bash shell
instead of the cygwin terminal
(both are under Cygwin group in the Start menu). They look very similar, but the font in the bash shell is less pretty and you cannot make the window wider than 80 chars (like all other CMD windows, never understood that 'feature'). This is cygwin bash running inside a CMD window, and your anaconda python will be interactive. The cygwin terminal
gives you a bash shell running in a putty derivative, which mimics an xterm and cannot be used by anaconda python.
解决此问题的一种方法是使用cygwin bash shell
而不是cygwin terminal
(两者都在“开始”菜单中的 Cygwin 组下)。它们看起来非常相似,但是 bash shell 中的字体不那么漂亮,并且您不能将窗口宽度设置为 80 个字符以上(就像所有其他 CMD 窗口一样,永远不会理解该“功能”)。这是在 CMD 窗口内运行的 cygwin bash,您的 anaconda python 将是交互式的。这cygwin terminal
为您提供了一个在 putty 衍生物中运行的 bash shell,它模仿 xterm,不能被 anaconda python 使用。
回答by SchizoBean
To work with the interactive Python shell in Cygwin I use the -i option.
为了在 Cygwin 中使用交互式 Python shell,我使用了 -i 选项。
To get it from the Anaconda install, I used the steps suggested above:
为了从 Anaconda 安装中获取它,我使用了上面建议的步骤:
$ export PATH=/cygdrive/c/anaconda:$PATH
$ which python
/cygdrive/c/anaconda/python
Then I launch python within Cygwin with the -i option:
然后我在 Cygwin 中使用 -i 选项启动 python:
$ python -i
Python 2.7.8 |Anaconda 2.1.0 (64-bit)| (default, Jul 2 2014, 15:12:11) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
>>>>
The libraries are also working fine. For instance the pandas library (which has been installed through Anaconda) can be imported now.
图书馆也工作正常。例如现在可以导入 pandas 库(已通过 Anaconda 安装)。
>>>> import pandas
>>>> pandas.DataFrame
<class 'pandas.core.frame.DataFrame'>
Now to make this permanent I added the path in my bashrc file:
现在为了使其永久化,我在 bashrc 文件中添加了路径:
$ echo 'export PATH=/cygdrive/c/anaconda:$PATH' >> .bashrc
$ source .bashrc
$ which python
/cygdrive/c/anaconda/python
I hope this helps.
我希望这有帮助。
回答by SciGuyMcQ
I too was having an issue getting anaconda set up with my Cygwin / Windows 7 system. What worked was the following:
我在使用 Cygwin/Windows 7 系统设置 anaconda 时也遇到了问题。有效的是以下内容:
Edited the ~/.bashrc. Add below to the bottom of file.
export PATH=/cygdrive/c/Anaconda:$PATH
This mapped Cygwin's python to the anaconda distribution as proof by entering
which python
in Cygwin's console. However, when launching python it would hang up forcing me to ^C out of the command. I found thatpython -i
will launch the interactive python interpreter with no issues.
编辑了 ~/.bashrc。将下面添加到文件底部。
export PATH=/cygdrive/c/Anaconda:$PATH
这通过输入
which python
Cygwin 的控制台将 Cygwin 的 python 映射到 anaconda 发行版作为证明。但是,在启动 python 时,它会挂断,迫使我 ^C 退出命令。我发现它python -i
可以毫无问题地启动交互式 python 解释器。
回答by Nicholas Fasolt
I'm a little late in coming to this, but I was having the same problem as you @thomasshouler. Try adding the export line below to the end of the .bash_profile and .profile files as well as your .bashrc file (as mentioned above). Make sure to "source .bash_profile" etc for each file.
我来得有点晚了,但我遇到了和你一样的问题@thomasshouler。尝试将下面的导出行添加到 .bash_profile 和 .profile 文件以及 .bashrc 文件(如上所述)的末尾。确保为每个文件“source .bash_profile”等。
export PATH=/cygdrive/c/anaconda:$PATH
回答by TKH
There is another problem with conda's interpretation of cygwin. When you source the activation script for an environment, it prepends e.g.
conda 对 cygwin 的解释还有一个问题。当您为某个环境提供激活脚本时,它会在前面加上例如
/C/Users/Thomas.Harris/AppData/Local/Continuum/Anaconda3/envs/blah
/C/Users/Thomas.Harris/AppData/Local/Continuum/Anaconda3/envs/blah/Library/mingw-w64/bin /C/Users/Thomas.Harris/AppData/Local/Continuum/Anaconda3/envs/blah/Library/usr/bin
/C/Users/Thomas.Harris/AppData/Local/Continuum/Anaconda3/envs/blah/Library/bin
/C/Users/Thomas.Harris/AppData/Local/Continuum/Anaconda3/envs/blah/Scripts
/C/Users/Thomas.Harris/AppData/Local/Continuum/Anaconda3/envs/blah
/C/Users/Thomas.Harris/AppData/Local/Continuum/Anaconda3/envs/blah/Library/mingw-w64/bin /C /Users/Thomas.Harris/AppData/Local/Continuum/Anaconda3/envs/blah/Library/usr/bin
/C/Users/Thomas.Harris/AppData/Local/Continuum/Anaconda3/envs/blah/Library/bin
/C /Users/Thomas.Harris/AppData/Local/Continuum/Anaconda3/envs/blah/Scripts
to your PATH. However, these are not proper directories in cygwin.
到您的路径。但是,这些不是 cygwin 中的正确目录。
I fixed this with
我用
ln -s /cygdrive/c /C
ln -s /cygdrive/c /C
and then if you create conda environments with specific versions of python they should work.
然后,如果您使用特定版本的 python 创建 conda 环境,它们应该可以工作。
回答by matthew
Found that for Anaconda 4, do:
发现对于 Anaconda 4,请执行以下操作:
vim /home/[Username]/.bash_profile
Append to the last line:
附加到最后一行:
PATH=$PATH:/cygdrive/c/Users/[Username]/Anaconda3/Scripts/
This solution worked for me as the usual anaconda3/bin could not be found.
这个解决方案对我有用,因为找不到通常的 anaconda3/bin。
回答by Marc Compere
Once the Cygwin path is updated as mentioned above, you can also add an alias in the .bashrc file:
如上所述更新 Cygwin 路径后,您还可以在 .bashrc 文件中添加别名:
alias python='python -i'
This will bring up the Anaconda python interpreter in interactive mode from a Cygwin bash prompt
这将从 Cygwin bash 提示符以交互模式调出 Anaconda python 解释器