代码之家  ›  专栏  ›  技术社区  ›  jsolum

打包多个Python文件:获取导入错误

  •  1
  • jsolum  · 技术社区  · 7 年前

    我正试图找出如何打包python项目,以便将其分发给一些同事。 当我跑步时 python setup.py sdist --formats=zip 它创造了一个拉链,我可以 pip install 但当我运行该文件时,它无法导入我创建的类文件。

    以下是我的项目文件结构(可能不正确,但我没有充分考虑到打包):

    ├── README.md
    ├── requirements.txt
    ├── scanner
    │   ├── __init__.py
    │   ├── __pycache__
    │   │   ├── googleSheets.cpython-35.pyc
    │   │   ├── merakiJsonHandler.cpython-35.pyc
    │   │   └── wifiTester.cpython-35.pyc
    │   ├── credentials.json
    │   ├── googleSheets.py
    │   ├── info.json
    │   ├── merakiJsonHandler.py
    │   ├── scan.py
    │   ├── whatWap.py
    │   └── wifiTester.py
    └── setup.py
    

    import setuptools
    
    setuptools.setup(name='att-scanner',
          version='0.1',
          description='Meraki Wap/Wifi Scanner',
          author='jsolum',
          author_email='*****',
          license='MIT',
          packages=setuptools.find_packages(),
          entry_points={
              'console_scripts' : [
                  'mws = scanner:scan.py',],},
          install_requires=['pyspeedtest', 'requests', 'gspread', 'oauth2client',],
          include_package_data=True)
    

    这是我的错误:

    Traceback (most recent call last):
      File "//anaconda/bin/mws", line 7, in <module>
        from scanner import scan
      File "//anaconda/lib/python3.5/site-packages/scanner/__init__.py", line 1, in 
    <module>
        from googleSheets import SheetsController
    ImportError: No module named 'googleSheets'
    

    为什么不能 scan.py 进口 googleSheets.py 我该怎么做才能让它导入这个类和我的其他类?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Peter Gibson    7 年前

    from googleSheets import SheetsController 是绝对导入语句,因此安装包后,需要使用包名称:

    from scanner.googleSheets import SheetsController
    

    或者 relative import statement

    from .googleSheets import SheetsController