如何pip安装本地python包?

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

How to pip install a local python package?

pythonpippython-import

提问by expz

Question

I installed a local package called credentialsusing

我安装了一个名为credentialsusing的本地包

pip install -e c:\users\worker\src\clockwork\lib\credentials

But when I try to import the package from a sibling directory, it fails with an ImporError:

但是当我尝试从同级目录导入包时,它失败并显示 ImporError:

cd c:\users\worker\src\clockwork\bank
python -c "import credentials"
...
ImportError: No module named 'credentials'

Confusingly, the package credentialsis listed as successfully installed as shown when I run pip list:

令人困惑的是,credentials当我运行时,该软件包被列为已成功安装pip list

...
credentials (1.0.0, c:\users\worker\src\clockwork\lib\credentials)
...

How can I install my local package so that it can be imported?

如何安装我的本地包以便可以导入?

Background

背景

I am using Python 3.4 (32-bit). The package contains two files:

我正在使用 Python 3.4(32 位)。该包包含两个文件:

credentials\__init__.py
credentials\setup.py

The __init__.pyfile defines a single function. The setup.pyfile is short:

__init__.py文件定义了一个函数。该setup.py文件很短:

from distutils.core import setup

setup(name='credentials', version='1.0.0')

Workaround

解决方法

I currently add the directory containing the package (c:\users\worker\src\clockwork\lib) to my PATHvariable as a workaround. But my question is how to install the package properly so that I do not need to modify the PATH.

我目前将包含包 ( c:\users\worker\src\clockwork\lib)的目录添加到我的PATH变量中作为解决方法。但我的问题是如何正确安装包,这样我就不需要修改PATH.

回答by Mr_and_Mrs_D

Uninstall the python package then install it using:

卸载 python 包,然后使用以下命令安装它:

python -m pip install -e c:\users\worker\src\clockwork\lib\credentials

What is probably happening is that you have multiple python installs and pip is run from one install while you are trying to use the package from another. See also:

可能发生的情况是您有多个 python 安装并且 pip 从一个安装运行,而您正在尝试使用另一个安装的包。也可以看看:

回答by expz

The problem centers on setup.py. It needs to declare a package:

问题集中在setup.py. 它需要声明一个包:

from distutils.core import setup

setup(name='credentials', version='1.0.0', packages=['credentials'])

But this setup.pymust be in the parent directory of the credentialspackage, so in the end, the directory structure is:

但是这个setup.py必须在credentials包的父目录下,所以最后的目录结构是:

...\credentials\setup.py
...\credentials\credentials\__init__.py

With this change, the module is found after reinstalling the package.

通过此更改,在重新安装包后找到该模块。

This could also be caused by two Python installs (but wasn't in my case), and @Mr_and_Mrs_D gives an answer for that case.

这也可能是由两次 Python 安装引起的(但在我的情况下不是),@Mr_and_Mrs_D 给出了这种情况的答案。