简体   繁体   English

Python - 虽然已安装但未找到包

[英]Python - package not found although it is installed

I have the following version of python我有以下版本的python

import sys
print(sys.version)

3.6.5 | packaged by conda-forge | (default, Apr  6 2018, 13:44:09) 
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)]

I installed a package with the following command我使用以下命令安装了一个包

pip install wfdb

It is succesfully installed because when I then write the command:它已成功安装,因为当我编写命令时:

pip show wfdb

The following information appears Location: /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages出现以下信息位置:/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages

However, when I type the command import wfdb in Python notebook or the version of python in terminal, I get the following message但是,当我在 Python notebook 中输入命令import wfdb或在终端中输入 python 版本时,我收到以下消息

No module named 'wfdb'

Does it have to do with the path on which python is checking where the packages are?它与python检查包在哪里的路径有关吗? How to check this and how to change it?如何检查这一点以及如何更改它?

You have (at least) 2 Python installations, one managed by Anaconda, the other what appears to be an official Python.org Mac build installed system-wide.您(至少)有 2 个 Python 安装,一个由 Anaconda 管理,另一个似乎是在系统范围内安装的官方 Python.org Mac 版本。 The pip command on the command-line is the one tied to the Python.org Mac build.命令行上的pip命令是与 Python.org Mac 构建相关的命令。

pip is a script that is tied to a specific Python installation, and there can be multiple versions of the script installed in different locations, and is usually also installed with pipX and pipX.Y to match the XY version indicator of the Python version it is tied to. pip是绑定到特定 Python 安装的脚本,并且可以在不同位置安装多个版本的脚本,并且通常还会安装pipXpipX.Y以匹配它所在 Python 版本的XY版本指示器拴。 For Python 3.6, that means the same script would also be available as pip3 and pip3.6 .对于 Python 3.6,这意味着相同的脚本也可用作pip3pip3.6 (This also means that pip can be connected to Python 2 or Python 3, depending on your exact OS setup. It is not a given that pip , without a version number, installs into Python 2.x as some answers may claim). (这也意味着pip可以连接到 Python 2 或 Python 3,具体取决于您的确切操作系统设置。正如某些答案可能声称的那样,没有版本号的pip安装到 Python 2.x 中并不是给定的)。

Note that when you run a command without a path in your shell, (such as pip as opposed to /usr/bin/pip ), you are asking your shell to find the command for you in a number of locations, listed in the PATH environment variable.请注意,当您在 shell 中运行没有路径的命令(例如pip而不是/usr/bin/pip )时,您是在要求 shell 在PATH列出的多个位置为您查找命令环境变量。 The first location in the PATH list with that command is then fixed.然后使用该命令修复PATH列表中的第一个位置。 which -a <command> would tell you all possible PATH -registered locations that the command can be found in. You can always use the full path to a command to bypass the PATH search path. which -a <command>会告诉您可以在其中找到命令的所有可能的PATH位置。您始终可以使用命令的完整路径来绕过PATH搜索路径。

You can always verify what Python version the pip command is connected to with:您始终可以验证pip命令连接到的 Python 版本:

pip -V

which will output the version of pip and the location it is installed with.这将输出pip的版本及其安装位置。 It'll print something like它会打印出类似的东西

pip pipX.pipY path/to/pythonX.Y/site-packages/pip (python X.Y)

where pipX.pipY is the pip version number and path/to/pythonX.Y tells you what Python installation this is for.其中pipX.pipY是 pip 版本号, path/to/pythonX.Y告诉您这是用于什么 Python 安装。

You can try to match this with the Python version by running您可以尝试通过运行将其与 Python 版本匹配

python -m site

which outputs the Python module search path for that Python version.它输出该 Python 版本的 Python 模块搜索路径。 Python can be run with python , pythonX and pythonX.Y too, and is subject to the same PATH search. Python 也可以使用pythonpythonXpythonX.Y运行,并且受相同的PATH搜索约束。

Note the -m switch there, that instructs Python to find a module in it's module search path and execute it as a script.请注意那里的-m开关,它指示 Python 在其模块搜索路径中查找模块并将其作为脚本执行。 Loads of modules support being run that way, including pip .大量模块支持以这种方式运行,包括pip This is important as that helps avoid having to search for a better pip command if you already can start the right Python version.这很重要,因为如果您已经可以启动正确的 Python 版本,这有助于避免搜索更好的pip命令。

You have several good options here:您有几个不错的选择:

  • Since you are using Anaconda, you could look for a conda package for the same project.由于您使用的是 Anaconda,因此您可以为同一项目寻找 conda 包。 There is such a package for wfdb . wfdb这样一个包 Install it with安装它

     conda install wfdb

    Anaconda aims to give you a wider software management experience that includes a broader set of software options than just the Python PyPI ecosystem, and conda packages usually manage more things than just the Python package. Anaconda 旨在为您提供更广泛的软件管理体验,其中包括比 Python PyPI 生态系统更广泛的软件选项集,并且 conda 包通常管理的不仅仅是 Python 包。

    Conda packages are usually maintained by a different set of developers from the package itself, so there may be a newer version available on PyPI (requiring pip install ) than there is on Conda. Conda 包通常由与包本身不同的一组开发人员维护,因此 PyPI 上可能有比 Conda 更新的版本(需要pip install )。

    This is not an option for all Python packages, if there is no conda package you have to use pip .这不是所有 Python 包的选项,如果没有conda包,您必须使用pip See Installing non-conda packages .请参阅安装非 conda 包

  • you can use theconda command to create a conda environment .您可以使用conda命令来创建conda 环境 Once you have an environment created, you can activate it with创建环境后,您可以使用以下命令激活它

     source activate <name_of_cenv>

    to alter your PATH settings.更改您的PATH设置。 With the envirnoment 'active' the first directory listed on your PATH is the one for the conda environment and the pip command will be the one tied to that environment.在环境“活动”的情况下, PATH列出的第一个目录是 conda 环境的目录,而pip命令将是与该环境相关联的目录。

    Note that a conda environment gives you an isolated environment for a specific project, keeping the library installation separate from the rest of your Python packages in the central site-packages location.请注意,conda 环境为特定项目提供了一个独立的环境,将库安装与中央site-packages位置中的其余 Python 包分开。 If you want to install a package for all of your Anaconda Python projects, don't use a conda environment.如果要为所有 Anaconda Python 项目安装一个包,请不要使用 conda 环境。

  • Use the Anaconda Python binary to run pip as a module;使用 Anaconda Python 二进制文件将pip作为模块运行; when you can run /path/to/python or pythoncommand to open the right Python version, you can use that same path to run /path/to/python -m pip ... instead of pip ... to be absolutely certain you are installing into the correct Python version.当您可以运行/path/to/pythonpythoncommand打开正确的 Python 版本时,您可以使用相同的路径运行/path/to/python -m pip ...而不是pip ...绝对确定您正在安装到正确的 Python 版本中。

  • Look for a better pip command, with which -a pip or which -a pip3.6 , etc. But if you already know the Python binary, look in the same bin location for pip .寻找更好的pip命令,使用which -a pipwhich -a pip3.6等。但是如果您已经知道 Python 二进制文件,请在相同的bin位置查找pip If you have anaconda/bin/python , then there probably is a anaconda/bin/pip too.如果您有anaconda/bin/python ,那么可能也有anaconda/bin/pip

As you can read here :正如你可以在这里阅读:

pip3 and pip would make a difference only when you are not using any environment managers like virualenv (or) conda. pip3 和 pip 只有在您不使用任何环境管理器(如 virualenv(或)conda)时才会有所作为。 Now as you are creating a conda environment which has python==3.x, pip would be equivalent to pip3.现在,当您创建一个具有 python==3.x 的 conda 环境时,pip 将等同于 pip3。

For this reason it could be you did not activate your Conda environment before installing required packages and running your code.出于这个原因,可能是您在安装所需的软件包和运行代码之前没有激活 Conda 环境。

Activate the new environment:激活新环境:

On Windows:在 Windows 上:

activate myenv

On macOS (this should be your option) and Linux:在 macOS(这应该是您的选择)和 Linux 上:

source activate myenv

NOTE: Replace myenv with the name of the environment.注意:将 myenv 替换为环境名称。

You have installed python2.x package and you're using python3.x.你已经安装了 python2.x 包并且你正在使用 python3.x。 Try:尝试:

pip3 install wfdb

If you don't have pip3 run:如果你没有pip3运行:

[apt-get/yum] install python3-pip

You can see what packages you have currently installed by running:您可以通过运行以下命令查看当前安装了哪些软件包:

pip freeze

and for python 3.x packages和 python 3.x 包

pip3 freeze

Please remember each time you install a Python package, it will be placed in the directory for one particular Python version.请记住,每次安装 Python 包时,它都会放在一个特定 Python 版本的目录中。 Hence your error.因此你的错误。

which python

gives the you the PATH to python给你PATH到 python

and then /path/to/python -m pip install thepackagetobeinstalled然后/path/to/python -m pip install thepackagetobeinstalled

Many thanks @MartijnPieters非常感谢@MartijnPieters

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

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