无法导入最近创建的包

0

这是我的存储库的结构:

├── README.md
├── core
│   ├── Dockerfile
│   ├── entrypoint.sh
│   ├── requirements.txt
│   ├── sentinel.py
│   └── site.conf
└── python_package
    ├── sentinel
    │   ├── __init__.py
    │   └── sentinel.py
    └── setup.py

它有两个目录。主要的是core保存服务本身的代码。我想创建一个帮助程序包来与此服务交互,因此我创建python_package并添加到sentinel.py一个帮助程序类 ( Sentinel)。__init__.py是空的。

内容setup.py为:

from setuptools import setup, find_packages


setup(name='sentinel',
      version='0.1',
      description='Client to interact with the Sentinel service',
      url='https://foo/bar/sentinel.git',
      author='yzT',
      author_email='[email protected]',
      packages=find_packages(),
      install_requires='requests')

当我激活 virtualenv 并 install 时pip install .,该软件包与requests依赖项一起安装如果那时我打开一个 python 终端并尝试导入from sentinel import Sentinel它报告的类:但ImportError: cannot import name 'Sentinel' from 'sentinel' (/Users/yzT/sentinel/python_package/sentinel/__init__.py)我不知道为什么。

塞尔吉·巴列斯塔

如果我正确理解了您的问题,则该包sentinel包含一个sentinel.py模块,而模块又包含一个Sentinel类。在这种情况下,您必须添加一个级别:

from sentinel.sentinel import Sentinel

或者,您可以通过其__init__.py文件将一些符号导入包中如果python_package/sentinel/__init__.py包含(请注意点 ( .)):

from .sentinel import Sentinel

那么Sentinel符号将直接存在于包中,您可以在外部脚本中使用:

from .sentinel import Sentinel

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章