gunicorn起動此項目時。
報錯:
File "/usr/local/python2.7/lib/python2.7/site-packages/gunicorn/workers/workertmp.py", line 12, in <module> PLATFORM = platform.system() AttributeError: 'module' object has no attribute 'system'
然后看源碼,platform是python自帶的包,沒有問題。最后經人指點,發現項目工程目錄中有一個包名是Platform,去掉platform目錄,就沒問題了。
所以自己寫的項目無論是包名還是py文件名,還是類或者方法名,千萬別跟Python自帶的重名!
6.1.2. The Module Search Path
When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:
- the directory containing the input script (or the current directory).
- PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
- the installation-dependent default.
After initialization, Python programs can modify sys.path. The directory containing the script being run is placed at the beginning of the search path, ahead of the standard library path. This means that scripts in that directory will be loaded instead of modules of the same name in the library directory. This is an error unless the replacement is intended. See section Standard Modules for more information.
當導入名為 spam 的模塊時, 解釋器會先在當前目錄下查找名為 spam.py 的文件, 然后搜索環境變量 PYTHONPATH 記錄的所有目錄. 這個變量與 shell 變量 PATH 有相同的語法, 它是一些目錄名的列表. 當沒有設置 PYTHONPATH , 或者在前述各種路徑中都沒找到期望的文件時, 那么解釋器會在pyhton 安裝時定義的默認目錄中搜尋. 在 Unix 中, 通常是在/usr/local/lib/python .
實際上, 都是在變量 sys.path 定義的目錄列表中搜索模塊的, 該變量初始化包含了執行腳本的目錄 (或者當前目錄) 以及 PYTHONPATH 和與安裝時相關的默認目錄. 這使得 Python 程序猿在必要的時候,可以直接進行更改或替代相關路徑來進行模塊搜索.
注意, 因為搜索目錄包含當前運行腳本的目錄, 所以令腳本名不與某個標准模塊重名很重要, 否則 Python 會嘗試把這個腳本當成一個模塊載入, 而重名系統模塊已經被導入時. 這通常會拋出一個錯誤. 參看 標准模塊 小節獲取更多信息.
PS:還有一種方式避免上述的問題,就是在每個py文件上的首行引入以下一行
from __future__ import absolute_import
也是可以解決上述問題。