Python 模块是在 OSX 上使用 pip 安装的,但在导入时找不到

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

Modules are installed using pip on OSX but not found when importing

pythonmacospython-2.7importmodule

提问by J0ANMM

I successfully install different modules using pip and they are shown in the

我使用 pip 成功安装了不同的模块,它们显示在

pip list

such as:

如:

beautifulsoup4 (4.4.1)
requests (2.10.0)
Scrapy (1.1.0)

From Terminal

从航站楼

However, whenever I try to import it

但是,每当我尝试导入它时

import beautifulsoup4/ import bs4or import Scrapyor import requests

import beautifulsoup4/import bs4import Scrapyimport requests

the following error is shown:

显示以下错误:

$ python
Python 2.7.5 (default, Mar  9 2014, 22:15:05) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named requests

Update:if I launch python when I am at the correct site-packages directory

更新:如果我在正确的 site-packages 目录下启动 python

$ pwd
/usr/local/lib/python2.7/site-packages
$ python
Python 2.7.5 (default, Mar  9 2014, 22:15:05)
>>> import requests
>>> import bs4
>>> import scrapy

Then it works. This would solve the issue if writing directly on the Terminal. However, I have no clue about how to make it work inside a file.py, which will be the normal use.

然后它起作用了。如果直接在终端上写入,这将解决问题。但是,我不知道如何使其在 file.py 中工作,这将是正常使用。

As far as I know, I only have Python2.7 installed.

据我所知,我只安装了 Python2.7。

From file.py

从文件.py

If I have a file.py saved in some local folder. This contains, for instance

如果我将 file.py 保存在某个本地文件夹中。这包含,例如

import requests
from bs4 import BeautifulSoup

when I try

当我尝试

python file.py

I get the same error.

我犯了同样的错误。

Approach

方法

Same happens with any other module from the list. I would think pip is installing them in a directory that Python is not reading, but as per what I read, it is the correct one.

列表中的任何其他模块也会发生同样的情况。我认为 pip 正在将它们安装在 Python 未读取的目录中,但根据我阅读的内容,它是正确的。

They are all installed here:

它们都安装在这里:

/usr/local/lib/python2.7/site-packages

Output requested by Padraic Cunningham:

Padraic Cunningham 要求的输出:

$ which -a pip
/usr/local/bin/pip
$ which -a python
/usr/bin/python
/usr/local/bin/python

Output requested by leovp:

leovp 请求的输出:

$ pip -V
pip 8.1.2 from /usr/local/lib/python2.7/site-packages (python 2.7)

Threads already checked

线程已检查

I have checked the following threads, but unfortunately they did not help me to solve the issue:

我检查了以下线程,但不幸的是它们没有帮助我解决问题:

Any ideas of what the problem is?

任何想法是什么问题?

采纳答案by Ani Menon

Since your problem maybe caused due to various reason, I have listed down a few of them here :

由于您的问题可能是由于各种原因引起的,我在这里列出了其中的一些:

The link you were looking for : https://pythonhosted.org/setuptools/setuptools.html#development-mode

您正在寻找的链接:https: //pythonhosted.org/setuptools/setuptools.html#development-mode

  • It may also happen if you have two versions of python installed. If the pipthat you are accessing is of one version & the python interpreter used is another.
  • 如果您安装了两个版本的 python,也可能会发生这种情况。如果pip您访问的是一个版本并且使用的 python 解释器是另一个版本。

So just see to that you are using the same version of python to install and use the package.

因此,请确保您使用相同版本的 python 来安装和使用该软件包。

You may fix this using alias,

您可以使用别名解决此问题,

First, set up a shell alias:

首先,设置一个shell别名:

alias python=/usr/local/bin/python3

Then, type that at a prompt, or put it in your ~/.bashrcso that whenever you open python from the terminal the correct version opens.

然后,在提示符下键入,或将其放入您的文件中,~/.bashrc以便每当您从终端打开 python 时,都会打开正确的版本。

  • If both of the above methods don't work for you then check this :
  • 如果上述两种方法都不适合您,请检查以下内容:

ImportError No module namedor this

ImportError No module namedor this

回答by J0ANMM

Here the answer that worked, which is basically what has been explained in the comments of the question. However, I thought it would be useful to have it explained as a clear and well structured answer.

这是有效的答案,这基本上是问题评论中已解释的内容。但是,我认为将其解释为清晰且结构良好的答案会很有用。

As highlighted, the problem was that I was not using the interpreter that pip was installing for. The command whichshows where pip was installing the modules:

正如所强调的,问题是我没有使用 pip 安装的解释器。该命令which显示 pip 安装模块的位置:

$ which -a pip
/usr/local/bin/pip

and where the different python versions were located:

以及不同的python版本所在的位置:

$ which -a python
/usr/bin/python
/usr/local/bin/python

That is, my system/default python was

也就是说,我的系统/默认 python 是

/usr/bin/python

while pip was installing for

当 pip 正在安装时

/usr/local/bin/python

Therefore, I could not import anything I installed when I just typed python, because the /usr/bin/pythoninterpreter was the one started.

因此,我无法导入我刚输入时安装的任何东西python,因为/usr/bin/python解释器是启动的。

Solution

解决方案

Install pip againspecifying the destination of the modules that will be installed. This must be the destination for the system/default python.

再次安装 pip指定将安装的模块的目的地。这必须是系统/默认 python 的目标。

This has been done in two steps:

这分两步完成:

  1. Downloding get-pip.pyfrom bootstrap.pypa.io/get-pip.py
  2. Installing it with the following command

    sudo /usr/bin/python get-pip.py

  1. Downlodingget-pip.pybootstrap.pypa.io/get-pip.py
  2. 使用以下命令安装它

    sudo /usr/bin/python get-pip.py

Note that without the sudoI got an error and was not able to install pip.

请注意,如果没有,sudo我会出错并且无法安装 pip。

回答by Raymond

I have just fixed a similar issue.

我刚刚修复了一个类似的问题。

To give some background, I install pipwith homebrewby executing brew install python. One drawback by executing this command, it will install both python2 and python3(maybe not a disadvantage in some case), then

为了让一些背景,我安装piphomebrew通过执行brew install python。执行此命令的一个缺点是,它会同时安装 python2 和 python3(在某些情况下可能不是缺点),然后

pip install scrapy

pip install scrapy

but when I try to import scrapy, it complained ImportError: No module named scrapy.

但是当我尝试时import scrapy,它抱怨了ImportError: No module named scrapy



My Solution:run brew doctor, it should report you a link is broken, it asks you to run brew link python, you might encounter some errors, but follow the prompt suggestion to move forward, after successfully executing brew link python, everything should work now.

我的解决方案:运行brew doctor,它应该报告您链接已损坏,它要求您运行brew link python,您可能会遇到一些错误,但按照提示继续前进,成功执行后brew link python,现在一切正常。

回答by Eric Wiener

I'm adding this in case it helps anyone else out. For me the issue was I was running Anaconda and pip3 was installing to a different directory than Anaconda was linked with. To fix this run conda deactivate. You can reactivate later with conda activate

我添加这个以防它帮助其他人。对我来说,问题是我正在运行 Anaconda 并且 pip3 安装到与 Anaconda 链接的目录不同的目录中。要修复此运行conda deactivate。您可以稍后重新激活conda activate

回答by user12334347

I was able to fix this problem by running:

我能够通过运行来解决这个问题:

$ brew doctor 

and got the following:

并得到以下信息:

Consider setting your PATH so that /usr/local/binoccurs before /usr/bin.

Here is a one-liner:

考虑设置您的 PATH 以便/usr/local/bin/usr/bin.

这是一个单行:

echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.bash_profile

Once I ran the one-liner, I was able to access the installed package from /usr/local/bin

一旦我运行了 one-liner,我就可以从 /usr/local/bin

回答by user73772

Run brew doctorin the terminal it should give you a warning that says:

brew doctor在终端中运行它应该给你一个警告说:

Warning: The following directories do not exist:
/usr/local/sbin

You should create these directories and change their ownership to your account.
  sudo mkdir -p /usr/local/sbin
  sudo chown -R $(whoami) /usr/local/sbin

type sudo mkdir -p /usr/local/sbinin the terminal and hit enter then type sudo chown -R $(whoami) /usr/local/sbinthen run python and try importing your module again.

键入sudo mkdir -p /usr/local/sbin在终端和回车键然后键入sudo chown -R $(whoami) /usr/local/sbin然后运行Python和再次尝试导入您的模块。