简体   繁体   English

如何pip安装本地python包?

[英]How to pip install a local python package?

Question

I installed a local package called credentials using 我安装了一个名为credentials的本地包

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 credentials is listed as successfully installed as shown when I run pip list : 令人困惑的是,当我运行pip list时,包credentials被列为已成功安装,如下所示:

...
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). 我使用的是Python 3.4(32位)。 The package contains two files: 该软件包包含两个文件:

credentials\__init__.py
credentials\setup.py

The __init__.py file defines a single function. __init__.py文件定义了一个函数。 The setup.py file is short: 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 PATH variable as a workaround. 我目前将包含该包的目录( c:\\users\\worker\\src\\clockwork\\lib )添加到我的PATH变量中作为解决方法。 But my question is how to install the package properly so that I do not need to modify the PATH . 但我的问题是如何正确安装软件包,以便我不需要修改PATH

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. 可能发生的事情是你有多个python安装,并且当你试图从另一个安装使用包时,从一个安装运行pip。 See also: 也可以看看:

The problem centers on setup.py . 问题集中在setup.py It needs to declare a package: 它需要声明一个包:

from distutils.core import setup

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

But this setup.py must be in the parent directory of the credentials package, 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给出了这种情况的答案。

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

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