切换 python 发行版
我有一台配备 Snow Leopard 的 MacBook Pro,以及标配的 Python 2.6 发行版。 Numpy 无法正常工作。 Loadtxt 给出文件名太长的错误,而 getfromtxt 根本不起作用(模块中没有对象错误)。然后我尝试在 MacPorts 上下载 py26-numpy 端口。当然,当我使用python时,它默认是mac发行版。我如何将其切换为使用 MacPorts 的最新、最好的版本。这看起来比从源代码构建我需要的所有工具要简单得多...
谢谢!
I have a MacBook Pro with Snow Leopard, and the Python 2.6 distribution that comes standard. Numpy does not work properly on it. Loadtxt gives errors of the filename being too long, and getfromtxt does not work at all (no object in module error). So then I tried downloading the py26-numpy port on MacPorts. Of course when I use python, it defaults the mac distribution. How can I switch it to use the latest and greatest from MacPorts. This seems so much simpler than building all the tools I need from source...
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,将 MacPorts 路径 (
/opt/local/bin
) 添加到$PATH
中。在.bashrc
(或您使用的任何 shell 配置文件)中:如果您通过 MacPorts 安装了多个版本的 Python,和/或想要在 MacPorts 和 Apple 发行版之间轻松切换,您可以安装 < a href="http://trac.macports.org/browser/trunk/dports/sysutils/python_select/Portfile" rel="nofollow noreferrer">python_select 端口。
另请注意,MacPorts 版本的 Python 2.6 安装在
/opt/local/bin/python2.6
中,因此要使用该解释器,您必须执行以下三件事之一:python2.6
(不仅仅是python
)。python
调用python2.6
(alias python=python2.6
)。/opt/local/bin/python
手动设置符号链接 ->/opt/local/bin/python2.6
。python_select
设置调用python
使用的Python。选项#3 或#4 可能是最好的选择。
First of all, add the MacPorts path (
/opt/local/bin
) to your$PATH
. In.bashrc
(or whatever shell config file you use):If you have multiple versions of Python installed via MacPorts, and/or want to easily switch between the MacPorts and Apple distributions, you can install the python_select port as well.
Also note that the MacPorts version of Python 2.6 is installed into
/opt/local/bin/python2.6
, so to use that interpreter, you'll have to do one of three things:python2.6
(not justpython
).python
callspython2.6
(alias python=python2.6
)./opt/local/bin/python
->/opt/local/bin/python2.6
.python_select
to set the Python used by callingpython
.Options #3 or #4 are probably the best bet.
您需要更新您的
PATH
,以便MacPorts中的内容位于标准系统目录的前面,例如export PATH=/opt/local/bin:/opt/local/sbin: /opt/local/Library/Frameworks/Python.framework/Versions/Current/bin/:$PATH
。更新:要特别注意
/opt/local/Library/Frameworks/Python.framework/Versions/Current/bin
在旧前面
路径值。
You need to update your
PATH
so that the stuff from MacPorts is in front of the standard system directories, e.g.,export PATH=/opt/local/bin:/opt/local/sbin:/opt/local/Library/Frameworks/Python.framework/Versions/Current/bin/:$PATH
.UPDATE: Pay special attention to the fact that
/opt/local/Library/Frameworks/Python.framework/Versions/Current/bin
is in front of your oldPATH
value.现有的答案非常有用,但我注意到它们都没有说明如何使更改坚持下去。如果您不熟悉 unix 命令行,这可能很重要。
首先,解释一下:在基于 UNIX 的操作系统中,shell 中的重要配置信息存储在称为环境变量的东西中。名为 PATH 的环境变量将您的 shell 定向到查找程序的位置列表。当您键入命令时,它从 PATH 变量的最左端开始,并在该文件夹中查找您尝试运行的程序。如果找到,则运行它;否则它会在下一个文件夹中查找。当安装了同一程序的多个版本时,可以使用 PATH 变量给予一个优先级。
要使用此功能,请将包含闪亮新版本的文件夹放在路径前面,如下所示:
要在 shell 的单个版本中进行此更改,您可以键入
要在打开的每个 shell 中进行更改,您需要指示您的 shell 在每次启动时设置此变量。有一个名为
.bashrc
的文件,另一个名为.bash_profile
的文件,bash 在启动时会读取该文件。.bashrc
文件通常用于包含所有 shell 的指令,而.bash_profile
用于仅包含交互式 shell 的指令。因此,要使此更改生效,您可以编辑/Users/yourname/.bashrc
以包含如下行:其作用是添加
/opt/local/bin
到路径变量的前面,同时保留路径的其余部分。如果该更改似乎不起作用,您需要通过将source $HOME/.bashrc
添加到.bash_profile
脚本来确保调用 .bashrc,或者只需将必要的行移至.bash_profile
中即可。The existing answers are quite useful, but I noticed that neither of them spell out how to make the change stick. If you're not familiar with the unix command line this might be important.
First, and explanation: In unix based operating systems, important configuration information in the shell is stored in things called environment variables. The environment variable called PATH directs your shell to a list of places to look for programs. When you type a command, it starts at the leftmost end of the PATH variable, and looks in that folder for the program you tried to run. If it finds it, it runs it; else it looks in the next folder. When you have multiple versions of the same program installed, you can use the PATH variable to give one precedence.
To make use of this, put the folder with the shiny new version in front of the path, like this:
To make this change in a single version of your shell, you can type
To make the change in every shell you open, you need to instruct your shell to set this variable every time it starts. There is a file called
.bashrc
, and another one called.bash_profile
that bash will read when it starts up. The.bashrc
file is generally used to contain instructions for all shells, and.bash_profile
is used to contain instructions only for interactive shells. So, to make this change stick, you can edit/Users/yourname/.bashrc
to include a line like this:What that does is add
/opt/local/bin
to the front of the path variable while leaving the rest of the path alone. If that change doesn't seem to work, you will need to either ensure .bashrc is getting called by addingsource $HOME/.bashrc
to your.bash_profile
script, or just move the necessary line into.bash_profile
.