2017-09-27 59 views
1

我想弄清楚如何打包Python項目,以便我可以將它分發給一些同事。 當我運行python setup.py sdist --formats=zip它創建一個zip,我可以pip install,但是當我運行該文件時,它不能導入我創建的類文件。打包多個Python文件:正在導入錯誤

這裏是我的項目文件結構(可能不是正確的,但我沒有完全構建這個項目時考慮包裝):

├── 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 

'scan.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什麼爲什麼可以做我做,使之導入和我的其他班?

回答

0

from googleSheets import SheetsController是一個絕對的import語句,所以一旦安裝你的包,你需要或者使用的軟件包名稱:

from scanner.googleSheets import SheetsController 

還是一個relative import statement

from .googleSheets import SheetsController 
+0

*捂臉*你是對的。我以爲我檢查了!目前我得到一個錯誤,說''sys.exit(掃描()) TypeError:'模塊'對象不可調用''。我很好奇它爲什麼試圖調用scan.py.掃描是否需要重命名爲__main__? (我想我在解決問題的時候先閱讀過類似的文章)。 –

+0

@JamesSolum我不是100%肯定的,但它看起來像你的入口點設置爲模塊,而不是模塊中的功能。試試像'scanner.scan:main' –

+0

我剛纔看到這個評論,但是是的,就是這樣!謝謝! –