简体   繁体   English

在OS X中安装python库时遇到问题(numpy,scipy,matplotlib)

[英]Trouble installing python libraries in OS X (numpy, scipy, matplotlib)

Questions similar to this have been asked before, but I only can find explanations of the problem, not solutions. 之前已经提出了与此类似的问题,但我只能找到问题的解释,而不是解决方案。

I am running OS X Mavericks, and learning python. 我正在运行OS X Mavericks,并学习python。 I installed Python but little did I know it was built in. 我安装了Python但很少知道它是内置的。

I use sublime text as my IDE, and I tried to install a few libraries (scipy, numpy, matplotlib). 我使用sublime文本作为我的IDE,我尝试安装一些库(scipy,numpy,matplotlib)。 In Sublime Text, when I import the libraries, they work. 在Sublime Text中,当我导入库时,它们可以工作。 I can import scipy and print scipy.pi perfectly. 我可以导入scipy并完美打印scipy.pi。 However, when I ran it in the terminal, it said it couldn't find those libraries. 但是,当我在终端中运行它时,它说它找不到那些库。

Others have explained that the terminal is running the operating system's python and couldn't find the library, but didn't explain where one should place the libraries for it to run properly in the terminal. 其他人已经解释说终端正在运行操作系统的python并且找不到库,但是没有解释应该在哪里放置库以使其在终端中正常运行。

Any help is appreciated, thank you 感谢任何帮助,谢谢

The short answer to your question is that you need to modify your PYTHONPATH environment variable to point to the modules that you want. 对您的问题的简短回答是,您需要修改PYTHONPATH环境变量以指向所需的模块。 If you're using bash (which you most likely are), you can do that like this: 如果你正在使用bash(你最有可能),你可以这样做:

export PYTHONPATH=$PYTHONPATH:'/path/to/your/modules/'

However, rather than running the OS X version of python, I would recommend that you run the python version that you installed. 但是,我建议你运行你安装的python版本,而不是运行OS X版本的python。 If you installed using the .dmg from python.org, you can probably just type 'python2.7' or 'python3' (depending on which version you installed) in the terminal and everything will just work. 如果您使用python.org中的.dmg安装,您可以在终端中输入'python2.7'或'python3'(取决于您安装的版本),一切都会正常工作。

If you want to, you can set the command 'python' to run the version of python that you installed rather than the OS X default python by putting in an alias or modifying your PATH environment variable in your ~/.bash_profile file. 如果你愿意,你可以设置命令'python'来运行你安装的python版本而不是OS X默认python,方法是在〜/ .bash_profile文件中输入别名或修改你的PATH环境变量。 The easier option is to use the "Update Shell Profile" script that lives in the python folder after the installation. 更简单的选择是在安装后使用python文件夹中的“Update Shell Profile”脚本。

One more possibility for you to mull over -- consider setting up a virtual environment with virtualenv , and installing scipy, numpy, etc. in there. 您可以考虑再考虑一下 - 考虑使用virtualenv设置虚拟环境,并在其中安装scipy,numpy等。 This option might be more trouble than it's worth when you're first starting out, but in the long run it's a good skill to have and will make your development cleaner and easier to maintain. 当你刚开始时,这个选项可能比它的价值更麻烦,但从长远来看,这是一项很好的技能,可以使你的开发更清洁,更容易维护。

Finally, since it sounds like you might be doing scientific computing, you could take a look at EPD free , which comes with many scientific packages preinstalled, and as a bonus will setup the necessary shell variables for you automatically. 最后,因为听起来你可能正在进行科学计算,你可以看看EPD free ,它预装了许多科学软件包,并且作为奖励将自动为你设置必要的shell变量。

You need to find out exactly what you are calling from Sublime Text. 你需要准确找出你从Sublime Text调用的内容。 This solution ONLY applies if what others are saying is true and Sublime Text is calling another python interpreter that is different from the one that you are calling from the shell. 此解决方案仅适用于其他人所说的是真的,而Sublime Text正在调用另一个与您从shell调用的python解释器不同的 python解释器。


Step 1 第1步

Quick and dirty way to find out what Sublime Text is calling: 快速而肮脏的方式来找出Sublime Text正在调用的内容:

  1. Make Python your build system. 使Python成为您的构建系统。 Select on menu bar: Tools > Build System > Python 在菜单栏上选择:工具>构建系统> Python
  2. Create new file (cmd+n) 创建新文件(cmd + n)
  3. Build (cmd+b) 构建(cmd + b)

This should give you an error message like this: 这应该给你一个这样的错误信息:

/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: can't find '__main__' module in ''
[Finished in 0.1s with exit code 1]
[shell_cmd: python -u ""]
[dir: /Users/someuser/Downloads]
[path: /usr/bin:/bin:/usr/sbin:/sbin]

The first line is giving you the path to python interpreter that Sublime Text is using. 第一行是为您提供Sublime Text正在使用的python解释器的路径。


Step 2 第2步

Next, you should find out what you are calling in the command line: 接下来,您应该在命令行中找到您要调用的内容:

% which python
/usr/local/bin/python

Using the information above, you can locate where /usr/local/bin/python is by: 使用上面的信息,您可以找到/ usr / local / bin / python所在的位置:

% ls -la /usr/local/bin/python
lrwxr-xr-x  1 someuser  wheel  33 Jul 29 23:14 /usr/local/bin/python@ -> ../Cellar/python/2.7.5/bin/python

If that the two paths are the same, then stop : my solution will not apply to your situation. 如果两条路径相同,则停止 :我的解决方案不适用于您的情况。


Step 3 第3步

If the two paths are different, then you will need to either specifically use the path to the python interpreter that sublime text is calling: 如果这两个路径不同的,那么你要么需要专门使用的路径,Python解释崇高文呼吁:

% echo 'print "hello"' | /usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
hello

Or, if you like to just use python to call in command line, then you can change the link that came up by "which python" (eg, /usr/local/bin). 或者,如果您只想使用python在命令行中调用,那么您可以更改“which python”(例如/ usr / local / bin)所带来的链接。

eg (DO NOT COPY AND PASTE THIS - please understand where the paths came from and why): 例如(不要复制和粘贴 - 请了解路径的来源和原因):

ln -s PATH_TO_YOUR_PYTHON /usr/local/bin/python ln -s PATH_TO_YOUR_PYTHON / usr / local / bin / python

For better discussion about changing default python on OSX, see " How to change which Python version gets used in Snow Leopard? ". 有关在OSX上更改默认python的更好讨论,请参阅“ 如何更改在Snow Leopard中使用的Python版本? ”。


Another thing - if you want to run different python from Sublime Text, then take a look at this . 另一件事 - 如果你想从Sublime Text运行不同的python,那么看看这个

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

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