代码之家  ›  专栏  ›  技术社区  ›  The Illusive Man

无法导入最近创建的包

  •  0
  • The Illusive Man  · 技术社区  · 7 年前

    ├── 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='yzT@example.com',
          packages=find_packages(),
          install_requires='requests')
    

    当我激活virtualenv并安装 pip install . ,该软件包随 requests from sentinel import Sentinel 据报道: ImportError: cannot import name 'Sentinel' from 'sentinel' (/Users/yzT/sentinel/python_package/sentinel/__init__.py) 我不知道为什么。

    1 回复  |  直到 7 年前
        1
  •  2
  •   Serge Ballesta    7 年前

    如果我正确理解了你的问题,这个包裹是 sentinel 它包含一个 sentinel.py 依次包含 Sentinel 班在这种情况下,必须添加一个级别:

    from sentinel.sentinel import Sentinel
    

    __init__.py 文件如果 python_package/sentinel/__init__.py 包含(请注意圆点( . )):

    from .sentinel import Sentinel
    

    然后 哨兵

    来自。sentinel导入sentinel