简体   繁体   English

在 OSX 上使用 pip 安装模块,但在导入时未找到

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

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 bs4 or import Scrapy or 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更新:如果我在正确的站点包目录中启动 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.如果我将 file.py 保存在某个本地文件夹中。 This contains, for instance这包含,例如

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 要求的 Output:

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

Output requested by leovp: leovp 请求的 Output:

$ 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?关于问题是什么的任何想法?

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.正如所强调的,问题是我没有使用 pip 安装的解释器。 The command which shows where pip was installing the modules:命令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/python interpreter was the one started.因此,当我输入python时,我无法导入我安装的任何东西,因为/usr/bin/python解释器是启动的。

Solution解决方案

Install pip again specifying the destination of the modules that will be installed.再次安装 pip指定将安装的模块的目的地。 This must be the destination for the system/default python.这必须是系统/默认 python 的目标。

This has been done in two steps:这分两步完成:

  1. Downloding get-pip.py from bootstrap.pypa.io/get-pip.py .bootstrap.pypa.io/get-pip.py下载get-pip.py (You may need to use the deprecated one for Python 2: bootstrap.pypa.io/2.7/get-pip.py ) (您可能需要为 Python 2 使用已弃用的一个: bootstrap.pypa.io/2.7/get-pip.py

  2. Installing it with the following command使用以下命令安装它

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

Note that without the sudo I got an error and was not able to install pip.请注意,如果没有sudo我会出错并且无法安装 pip。

I have just fixed a similar issue.我刚刚修复了一个类似的问题。

To give some background, I install pip with homebrew by executing brew install python .为了提供一些背景知识,我通过执行brew install python使用homebrew安装pip One drawback by executing this command, it will install both python2 and python3(maybe not a disadvantage in some case), then执行此命令的一个缺点是,它会同时安装 python2 和 python3(在某些情况下可能不是缺点),然后

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 ,一切正常现在。

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 .如果您安装了两个版本的 python ,也可能会发生这种情况。 If the pip that you are accessing is of one version & the python interpreter used is another.如果您访问的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 ~/.bashrc so 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 named or this ImportError No module named or this

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.对我来说,问题是我正在运行 Anaconda 并且 pip3 安装到与 Anaconda 链接的目录不同的目录中。 To fix this run conda deactivate .要解决此问题,请运行conda deactivate You can reactivate later with conda activate您可以稍后使用conda activate重新conda activate

I was able to fix this problem by running:我能够通过运行来解决这个问题:

$ brew doctor 

and got the following:并得到以下内容:

Consider setting your PATH so that /usr/local/bin occurs before /usr/bin .考虑设置您的 PATH 以便/usr/local/bin出现在/usr/bin之前。

Here is a one-liner:这是一个单行:

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访问已安装的包

In my case I changed the VSCode interpreter to ~/opt/anaconda3/bin/python.在我的例子中,我将 VSCode 解释器更改为 ~/opt/anaconda3/bin/python。 This fixed the issue.这解决了这个问题。

Run brew doctor in 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/sbin in the terminal and hit enter then type sudo chown -R $(whoami) /usr/local/sbin then run python and try importing your module again.在终端中输入sudo mkdir -p /usr/local/sbin并按回车键,然后输入sudo chown -R $(whoami) /usr/local/sbin然后运行 ​​python 并再次尝试导入您的模块。

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

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