Python 提供的 Python OSX $ /Library/Frameworks/Python.framework/Versions/2.7/bin/python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23329034/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:45:52  来源:igfitidea点击:

Python OSX $ which Python gives /Library/Frameworks/Python.framework/Versions/2.7/bin/python

pythonmacospython-2.7twisted

提问by Nicolas Manzini

Hello I'm trying to run twistedalong with python but python cannot find twisted.

您好,我正在尝试与 python 一起运行twisted,但 python 找不到twisted

I did run $pip install twistedsuccessfully but it is still not available.

我确实成功运行了$pip install twisted但它仍然不可用。

ImportError: No module named twisted.internet.protocol

导入错误:没有名为twisted.internet.protocol 的模块

It seems that most people have $which pythonat /usr/local/bin/python

似乎大多数人 在/usr/local/bin/python 中都有$which python

but I get /Library/Frameworks/Python.framework/Versions/2.7/bin/python

但我得到/Library/Frameworks/Python.framework/Versions/2.7/bin/python

May this be the issue? If so, how can I change the PATH env?

这可能是问题吗?如果是这样,我该如何更改 PATH 环境?

回答by Mike Lutz

By the path your giving for OS X python I'm guessing your a rev-or-so old on your OS X (leopard?) so I can't directly compare with my machine.

根据您为 OS X python 提供的路径,我猜测您的 OS X(豹子?)上的版本或多或少,所以我无法直接与我的机器进行比较。

But, adding packages to the base OS X install is always a touchy thing, one check I would recommend is the permissions on any packages you add. Do a ls -l /Library/Python/2.7/site-packages/and make sure everything has rrights (and xrights for directories) (I.E. -rwxr-xr-xor drwxr-xr-x).

但是,将软件包添加到基本的 OS X 安装始终是一件棘手的事情,我建议您检查一下您添加的任何软件包的权限。执行ls -l /Library/Python/2.7/site-packages/并确保所有内容都具有r权限(和x目录权限)(IE-rwxr-xr-xdrwxr-xr-x)。

I had a recent case where a sudo pipwouldn't set user read rights on installed packages, and I believe "No module" was the error I was getting when I tried to use them

我最近遇到了一个案例,即sudo pip不会为已安装的软件包设置用户读取权限,我相信“无模块”是我尝试使用它们时遇到的错误

Because adding packages is so touchy on OS X, there are tons of guide on the net to doing hand installs of python. The first one I matched on a google is Installing / Updateing Python on OS X(use at your own risk, I personally haven't followed that guide)

因为在 OS X 上添加软件包非常棘手,所以网上有大量关于手动安装 python 的指南。我在谷歌上匹配的第一个是在 OS X 上安装/更新 Python(使用风险自负,我个人没有遵循该指南)

(... the 3rd part install system Brewis a very common method for people to do automated installs of python as well)

(...第三部分安装系统Brew也是人们进行python自动安装的一种非常常见的方法)

回答by Nicolas Manzini

Okay well in the terminal I finally found out:

好吧,在终端我终于发现:

open .bash_profile located at your user root (simply do a $cdin terminal to go there) and add where the path is the location of twisted

打开位于您的用户根目录下的 .bash_profile(只需$cd在终端中执行一个即可)并添加路径是扭曲的位置的位置

PYTHONPATH="/usr/local/lib/python2.7/site-packages:$PYTHONPATH" 
export PYTHONPATH

回答by Feng Han

It is just fine. Python may be installed in multiple places in your computer. When you get a new Mac, the default python directory may be

这很好。Python 可能安装在您计算机的多个位置。当你拿到一台新 Mac 时,默认的 python 目录可能是

 'usr/bin/python2.7'

You may also have a directory

您可能还有一个目录

'System/Library/Frameworks/Python.framework/Versions/2.7/bin/python'

The first one is the symlink of the second one.

第一个是第二个的符号链接。

If you use HomeBrew to install python, you may get a directory in

如果你使用 HomeBrew 安装 python,你可能会得到一个目录

'usr/local/bin/python2.7'

You may also have a directory as

您可能还有一个目录

'Library/Frameworks/Python.framework/Versions/2.7/bin/python'

which is exactly where my directory is.

这正是我的目录所在的位置。

The difference between the second one and the fourth one, you may find it here Installing Your Framework

第二个和第四个的区别,你可以在这里找到 安装你的框架

In your question, as you mentioned pip install is successful, but the installed packages still not available. I may guess your pip directory is not in your default python directory, and the packages are installed where your pip directory is. (Please use 'which pip'to check it out)

在您的问题中,正如您提到的 pip install 成功,但已安装的软件包仍然不可用。我可能猜测您的 pip 目录不在您的默认 python 目录中,并且软件包安装在您的 pip 目录所在的位置。(请使用'which pip'以查看)

For example, in my computer, the default pip directory is

比如我的电脑,默认的pip目录是

/Library/Frameworks/Python.framework/Versions/2.7/bin/pip

though, I have also pip in usr/local/bin.

不过,我在 usr/local/bin 中也有 pip。

So, all my packages installed via 'pip install' are stored in

因此,我通过“pip install”安装的所有软件包都存储在

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages

Hope that resolves your doubt. Similar things have happened to me, and it took me a whole night to figure out.

希望能解决你的疑惑。类似的事情也发生在我身上,我花了整整一个晚上才弄明白。

Here is the solution: Use PYTHONPATH="/usr/local/lib/python2.7/site-packages:$PYTHONPATH"to modify your python directory, or modify your pip directory. However, I would recommend a better way, use virtualenv. This can isolates Python environments, and can help you easily set up packages for each project.

解决方法如下:PYTHONPATH="/usr/local/lib/python2.7/site-packages:$PYTHONPATH"用于修改你的python目录,或者修改你的pip目录。但是,我会推荐一个更好的方法,使用virtualenv。这可以隔离 Python 环境,并可以帮助您轻松地为每个项目设置包。

回答by sand

I too was getting a ImportError: No module named xxxeven though I did a pip install xxxand pip2 install xxx. pip2.7 install xxxworked for me. This installed it in the python 2.7 directory.

ImportError: No module named xxx即使我做了一个pip install xxx和,我也得到了一个pip2 install xxxpip2.7 install xxx为我工作。这将它安装在 python 2.7 目录中。