简体   繁体   English

在Cygwin中永久设置Anaconda的Python路径

[英]Permanently set Python path for Anaconda within Cygwin

I'm trying to install Anaconda on my Windows 7 machine. 我正在尝试在我的Windows 7机器上安装Anaconda。 I often use cygwin to for my command-line work, and I would like to manage Anaconda from there. 我经常使用cygwin来完成我的命令行工作,我想从那里管理Anaconda。 I've worked through the graphic installer without any issues, and checked necessary boxes to reset my default path to this install of python. 我已经通过图形安装程序没有任何问题,并检查了必要的框以重置我的python安装的默认路径。 I go ahead to check where python is and initially I get this... 我继续检查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... 但我正试图从蟒蛇工作,所以我只需要重新定义我的道路......

$ 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. 我遇到的另一个(可能是相关的)问题是每次关闭/打开cygwin时都需要重置路径。 I've entered the following text into .bashrc and .profile to try and set the path permanently: 我已将以下文本输入.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? 这可能与设置某些系统环境变量有关吗?

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: 然后我用-i选项在Cygwin中启动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. 我希望这有帮助。

Path 路径

Regarding your path issue, see Installing anaconda to use with windows on how to fix that. 关于您的路径问题,请参阅安装anaconda以与Windows一起使用以了解如何解决该问题。 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. 或者,你可以运行/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. 请注意,互联网上提供的建议中很少也包括对PYTHONPATH的必要更改:如果您使用anaconda / windows python,您还需要anaconda python库。 Binary modules like numpy will surely crash or misbehave if you mix things up. numpy这样的二进制模块肯定会崩溃或行为异常,如果你混淆了。 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. 检查这个的最简单方法是在cygwin中安装一个模块,在Conda中不需要它并测试是否可以从anaconda python导入它。 If you can, your path is wrong. 如果可以,你的道路是错的。

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. Anaconda python会自动将c:\\Anconda添加到你的PYTHONPATH中,但是如果你将它设置为服务CYGWIN python库,那么它们将在Anaconda libs之前找到。 Try 尝试

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. 然而,这也是一个终端问题:Anaconda(Windows)python期望一个Windows shell和一个现代的cygwin shell就像一个xterm ,所以非常不同。 Diagnose this by running python --help , if that works but plain python hangs, the interactive prompt is the problem. 通过运行python --help诊断这个,如果它工作但普通的python挂起,交互式提示就是问题。 This means you can run python programs, you just cannot interact with them. 这意味着你可以运行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). 解决这个问题的方法是使用cygwin bash shell而不是cygwin terminal (两者都在开始菜单中的Cygwin组下)。 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'). 它们看起来非常相似,但是bash shell中的字体不太漂亮,你不能使窗口宽于80个字符(就像所有其他CMD窗口一样,永远不会理解'功能')。 This is cygwin bash running inside a CMD window, and your anaconda python will be interactive. 这是在CMD窗口内运行的cygwin bash,你的anaconda python将是交互式的。 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 terminal为你提供了一个运行在putty衍生物中的bash shell,它模仿xterm并且不能被anaconda python使用。

I too was having an issue getting anaconda set up with my Cygwin / Windows 7 system. 我也遇到了使用我的Cygwin / Windows 7系统设置anaconda的问题。 What worked was the following: 有用的是以下内容:

  1. Edited the ~/.bashrc. 编辑了〜/ .bashrc。 Add below to the bottom of file. 将以下内容添加到文件底部。

    export PATH=/cygdrive/c/Anaconda:$PATH

  2. This mapped Cygwin's python to the anaconda distribution as proof by entering which python in Cygwin's console. 这将Cygwin的python映射到anaconda发行版,作为在Cygwin控制台中输入which python证明。 However, when launching python it would hang up forcing me to ^C out of the command. 但是,当启动python时,它会挂断,迫使我从命令中删除^ C. I found that python -i will launch the interactive python interpreter with no issues. 我发现python -i将启动交互式python解释器而没有任何问题。

There is another problem with conda's interpretation of cygwin. conda对cygwin的解释还有另一个问题。 When you source the activation script for an environment, it prepends eg 当您为环境获取激活脚本时,它会预先例如

/C/Users/Thomas.Harris/AppData/Local/Continuum/Anaconda3/envs/blah /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/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/Library/bin
/C/Users/Thomas.Harris/AppData/Local/Continuum/Anaconda3/envs/blah/Scripts /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

and then if you create conda environments with specific versions of python they should work. 然后,如果你创建具有特定版本的python的conda环境,他们应该工作。

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. .profile(其他名称也有效,请参阅bash手册页)包含bash命令。 It is executed when bash is started as login shell. 当bash作为登录shell启动时执行。

At the bottom of this file, put your set path command: 在此文件的底部,输入您的set path命令:

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的输出是什么?

I'm a little late in coming to this, but I was having the same problem as you @thomasshouler. 我来这里有点晚了,但我和@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). 尝试将下面的导出行添加到.bash_profile和.profile文件的末尾以及.bashrc文件(如上所述)。 Make sure to "source .bash_profile" etc for each file. 确保为每个文件“source .bash_profile”等。

export PATH=/cygdrive/c/anaconda:$PATH

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。

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解释器

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

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