[英]My package installed with pip install shows no modules
I have a simple Python project with effectively one package (called forcelib
) containing one module (also called forcelib
): 我有一个简单的Python项目,其中forcelib
包含一个包含一个模块(也称为forcelib
)的软件包(称为forcelib
):
- setup.py
- forcelib
|- __init__.py
|- forcelib.py
My setup.py
is copied from the official example and has the obvious edits. 我的setup.py
是从官方示例复制而来的,具有明显的修改。
The problem is that I can install the forcelib
package using pip
but when I import forcelib
, it only has the "double-underscore" attributes visible. 问题是我可以使用pip
安装forcelib
软件包,但是在导入forcelib
,它仅具有可见的“ double-underscore”属性。 That is, I cannot see the forcelib
module. 也就是说,我看不到forcelib
模块。
Example to replicate: 复制示例:
git clone https://github.com/blokeley/forcelib
cd forcelib
pip install -e .
python
import forcelib
print(forcelib.__version__)
# Correctly prints 0.1.2 print(forcelib.__version__)
#正确打印0.1.2 dir(forcelib)
# The only contents are the __version__
, __path__
etc. double-underscore attributes. dir(forcelib)
#唯一的内容是__version__
, __path__
等。双下划线属性。 I had expected to see forcelib
, example_read
etc. 我曾期望看到forcelib
, example_read
等。 Perhaps I'm supposed to distribute just the module rather than bother with a package. 也许我应该只分发模块,而不要麻烦软件包。
The (very small) project is on GitHub . (很小) 项目在GitHub上 。
Any advice would be much appreciated. 任何建议将不胜感激。
It seems that there are 2 ways of doing it: 看来有两种方法可以做到:
Keep the same directory structure but put the following in __init__.py
保持相同的目录结构,但将以下内容放在__init__.py
from .forcelib import * 从.forcelib导入*
Distribute a module, not a package. 分发模块,而不是程序包。 Follow the instructions to use the py_modules argument rather than the packages
argument in setup.py
. 请按照说明使用py_modules参数而不是setup.py
的packages
参数。 This would mean restructuring the project to: 这意味着将项目重组为:
Approach (1) can be seen here . 方法(1) 可以在这里看到 。 It has the advantage of hiding the private functions and attributes (anything not in __all__
), but the client can still see the module forcelib.forcelib
, which I don't think it should. 它具有隐藏私有功能和属性(不在__all__
任何内容)的优点,但是客户端仍然可以看到模块forcelib.forcelib
,我认为这不是应该的。
Approach (2) can be seen here . 方法(2) 可以在这里看到 。 It is simpler, but has the disadvantage that it does not hide private functions and attributes. 它比较简单,但是缺点是它不隐藏私有函数和属性。
Download zip file and extract zip after go to forcelib-master directory then open command prompt and go to forcelib-master directory in command prompt and run command 下载zip文件并解压缩zip,然后转到forcelib-master目录,然后打开命令提示符并转到命令提示符中的forcelib-master目录并运行命令
python setup.py install
It will install packages successfully 它将成功安装软件包
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.