未找到 Python pip 安装模块。如何将python链接到pip位置?

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

Python pip install module is not found. How to link python to pip location?

pythonmodulepathinstallationhomebrew

提问by user1953478

I'm a newbie and I needed the pySerial and feedparser module for my projects. I'm running Mountain lion.

我是新手,我的项目需要 pySerial 和 feedparser 模块。我在跑山狮。

I followed the following tutorial so that I could upgrade to python 2.7.3 and then use the above mentioned modules.

我遵循了以下教程,以便我可以升级到 python 2.7.3,然后使用上述模块。

http://hackercodex.com/guide/python-virtualenv-on-mac-osx-mountain-lion-10.8/

http://hackercodex.com/guide/python-virtualenv-on-mac-osx-mountain-lion-10.8/

I followed this tutorial till I installed pip. Instead of installing Virtualenv. I used the following commands to install pySerial and feedparser

我一直跟着这个教程,直到我安装了 pip。而不是安装 Virtualenv。我使用以下命令安装 pySerial 和 feedparser

$ pip install pySerial
Requirement already satisfied (use --upgrade to upgrade): pySerial in /Library/Python/2.7/site-packages
Cleaning up...

I assumed that this was already present and checked it. Python seems to be importing this just fine. My python version has been upgraded to 2.7.3 btw since I installed it using homebrew as mentioned in the tutorial.

我认为这已经存在并检查了它。Python 似乎可以很好地导入它。自从我使用教程中提到的自制软件安装它以来,我的 python 版本已经升级到 2.7.3 btw。

Then I tried installing feedparser

然后我尝试安装 feedparser

$ pip install feedparser
Requirement already satisfied (use --upgrade to upgrade): feedparser in /usr/local/lib/python2.7/site-packages
Cleaning up...

Notice how its in the site-packages directory in the usr/local/lib.

请注意它在 usr/local/lib 中的 site-packages 目录中的情况。

All of my pip installs are being installed in that directory but python does not seem to be picking them up when i try importing them.

我所有的 pip 安装都安装在该目录中,但是当我尝试导入它们时,python 似乎没有选择它们。

How do I set the path so that python also looks there as well as core directory?

如何设置路径,以便 python 也可以在那里查看以及核心目录?

Your help will be greatly appreciated.

对你的帮助表示感谢。

I tried looking for answers here: Pip installs but module is not foundWhy I can't import beautifulsoup on mac using python 2.7 after installing it by using pip and/or easy_install?

我尝试在此处寻找答案: Pip 安装但未找到模块为什么我无法在使用 pip 和/或 easy_install 安装后使用 python 2.7 在 mac 上导入 beautifulsoup?

but niether of them are in the same situation as I am. I don't understand why this is happening as i edited my bash_profile with the following

但他们都没有和我一样。我不明白为什么会发生这种情况,因为我使用以下内容编辑了我的 bash_profile

# Set architecture flags
export ARCHFLAGS="-arch x86_64"
# Ensure user-installed binaries take precedence
export PATH=/usr/local/share/python:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
# Load .bashrc if it exists
test -f ~/.bashrc && source ~/.bashrc

then installed homebrew and then installed python 2.7.3 through homebrew (2.7.3 is now currently running on my machine)

然后安装自制软件,然后通过自制软件安装python 2.7.3(2.7.3目前正在我的机器上运行)

I figured all pip installs would be correctly linked?

我认为所有 pip 安装都会正确链接?

采纳答案by Ruslan

As a quick workaround, and assuming that you are on a bash-like terminal (Linux/OSX), you can try to export the PYTHONPATH environment variable:

作为一种快速解决方法,假设您使用的是类似 bash 的终端 (Linux/OSX),您可以尝试导出 PYTHONPATH 环境变量:

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

For Python 2.7

对于 Python 2.7

回答by Akash

how did you install easy_install/pip? make sure that you installed it for the upgraded version of python. what could have happened here is that the old (default) python install might be linked to your pip install. you might wanna try running the default version and importing the newly installed modules.

你是如何安装easy_install/pip的?确保你为升级版的python安装了它。这里可能发生的情况是旧的(默认)python 安装可能链接到您的 pip 安装。您可能想尝试运行默认版本并导入新安装的模块。

回答by boshea

I also had this problem. I noticed that all of the subdirectories and files under /usr/local/lib/python2.7/dist-packages/ had no read or write permission for group and other, and they were owned by root. This means that only the root user could access them, and so any user that tried to run a Python script that used any of these modules got an import error:

我也有这个问题。我注意到 /usr/local/lib/python2.7/dist-packages/ 下的所有子目录和文件都没有对组和其他人的读或写权限,它们归 root 所有。这意味着只有 root 用户可以访问它们,因此任何尝试运行使用这些模块的 Python 脚本的用户都会收到导入错误:

$ python
Python 2.7.3 (default, Apr 10 2013, 06:20:15) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import selenium
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named selenium
>>> 

I granted read permission on the files and search permission on the subdirectories for group and other like so:

我授予了对文件的读取权限和对组和其他子目录的搜索权限,如下所示:

$ sudo chmod -R go+rX /usr/local/lib/python2.7/dist-packages

And that resolved the problem for me:

这为我解决了问题:

$ python
Python 2.7.3 (default, Apr 10 2013, 06:20:15) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import selenium
>>> 

I installed these packages with pip (run as root with sudo). I am not sure why it installed them without granting read/search permissions. This seems like a bug in pip to me, or possibly in the package configuration, but I am not very familiar with Python and its module packaging, so I don't know for sure. FWIW, all packages under dist-packages had this issue. Anyhow, hope that helps.

我用 pip 安装了这些包(以 root 身份运行 sudo)。我不确定为什么它在没有授予读取/搜索权限的情况下安装它们。这对我来说似乎是 pip 中的一个错误,或者可能是包配置中的一个错误,但我对 Python 及其模块打包不是很熟悉,所以我不确定。FWIW,dist-packages 下的所有包都有这个问题。无论如何,希望有帮助。

Regards.

问候。

回答by sorin

Here is something I learnt after a long time of having issues with pip when I had several versions of Python installed (valid especially for OS X users which are probably using brew to install python blends.)

这是我在安装了多个版本的 Python 后长时间遇到 pip 问题后学到的东西(尤其适用于可能使用 brew 安装 python blends 的 OS X 用户。)

I assume that most python developers do have at the beginning of their scripts:

我假设大多数 python 开发人员在他们的脚本开头都有:

#!/bin/env python

You may be surprised to find out that this is not necessarily the same python as the one you run from the command line >python

您可能会惊讶地发现这与您从命令行运行的 Python 不一定相同 >python

To be sure you install the package using the correct pip instance for your python interpreter you need to run something like:

为确保您使用正确的 pip 实例为您的 python 解释器安装包,您需要运行如下内容:

>/bin/env python -m pip install --upgrade mymodule

回答by Cory Klein

No other solutions were working for me, so I tried:

没有其他解决方案对我有用,所以我尝试了:

pip uninstall <module> && pip install <module>

And that resolved it for me. Your mileage may vary.

这为我解决了它。你的旅费可能会改变。

回答by yask

For me the problem was that I had weird configuration settings in file pydistutils.cfg

对我来说,问题是我在文件中有奇怪的配置设置 pydistutils.cfg

Try running rm ~/.pydistutils.cfg

尝试跑步 rm ~/.pydistutils.cfg

回答by adc

If your pythonand pipbinaries are from different versions, modules installed using pip will not be available to python.

如果你pythonpip二进制文件来自不同的版本,使用 pip 安装的模块将无法用于 python。

Steps to resolve:

解决步骤:

  1. Open up a fresh terminal with a default environment and locate the binaries for pipand python.
  1. 打开一个默认的环境,新鲜的终端并找到二进制文件pippython
readlink $(which pip)
../Cellar/python@2/2.7.15_1/bin/pip

readlink $(which python)
/usr/local/bin/python3      <-- another symlink

readlink /usr/local/bin/python3
../Cellar/python/3.7.2/bin/python3

Here you can see an obvious mismatch between the versions 2.7.15_1and 3.7.2in my case.

在这里你可以看到版本2.7.15_13.7.2我的情况之间明显的不匹配。

  1. Replace the pip symlink with the pip binary which matches your current version of python. Use your python version in the following command.
  1. 将 pip 符号链接替换为与您当前版本的 python 匹配的 pip 二进制文件。在以下命令中使用您的 python 版本
ln -is /usr/local/Cellar/python/3.7.2/bin/pip3 $(which pip)

The -iflag promts you to overwrite if the target exists.

-i如果目标存在,该标志会提示您进行覆盖。

That should do the trick.

这应该够了吧。

回答by Solarcloud

For the sake of anyone also using visual studio from a windows environment:

为了任何也在 Windows 环境中使用 Visual Studio 的人:

I realized that I could see my module installed when i ran pip install

我意识到当我运行 pip install 时我可以看到我的模块安装

py pip install [moduleName] 
py pip list

However debugging in visual studio was getting "module not found". Oddly, i was successfully running import [moduleName]when i ran the interpreter in powershell.

然而,在 Visual Studio 中进行调试时出现“找不到模块”。奇怪的是,import [moduleName]当我在 powershell 中运行解释器时,我成功运行了。

Reason:

原因:

visual studio was using the wronginterpreter at: C:\Users\[username]\AppData\Local\Programs\Python\Python37\

Visual Studio 在以下位置使用了错误的解释器: C:\Users\[username]\AppData\Local\Programs\Python\Python37\

What I REALLY wanted was visual studio to use the virtualenv that i setup for my project. To do this, right click Python Environments in "solution explorer", select Add Virtual Environment..., and then select the folder where you created your virtual environment. enter image description hereThen, under project settings, under the General tab, select your virtual environment in the dropdown.

我真正想要的是 Visual Studio 使用我为我的项目设置的 virtualenv。为此,请右键单击“解决方案资源管理器”中的 Python 环境,选择添加虚拟环境...,然后选择您创建虚拟环境的文件夹。 在此处输入图片说明然后,在项目设置下的常规选项卡下,在下拉列表中选择您的虚拟环境。

enter image description here

在此处输入图片说明

Now visual studio should be using the same interpreter and everything should play nice!

现在visual studio应该使用相同的解释器,一切都应该很好!

回答by SuperNova

Below steps helped me fix this.

以下步骤帮助我解决了这个问题。

  • upgrade pip version
  • remove the created environment by using command rm -rf env-name
  • create environment using command python3 -m venv env-aide
  • now install the package and check
  • 升级pip版本
  • 使用命令删除创建的环境 rm -rf env-name
  • 使用命令创建环境 python3 -m venv env-aide
  • 现在安装包并检查

回答by Divyam Solanki

Make sure to check the python version you are working on if it is 2 then only pip install works If it is 3. something then make sure to use pip3 install

确保检查您正在使用的 python 版本,如果它是 2,则只有 pip install 有效如果是 3。那么一定要使用 pip3 install