![](/img/trans.png)
[英]Importing Python modules when there is a sibling directory with the same name
[英]Importing modules within the same directory
考慮以下目錄結構:
lib/
markov.py
solver.py
test.py
markov.py
具有以下代碼: super()在Sublime Text中引發錯誤,在PyCharm / Terminal中有效
solver.py
僅包含一堆數學函數,這是一個例子:
def mm1_busy_idle(arrival, service):
return round(1 - (arrival / service), 4)
現在,當我嘗試在markov.py
import solver
時,PyCharm告訴我, No module named solver
的No module named solver
。 但是,當我執行import test
它做得很好,我可以從那里運行測試功能:
def test():
print("test!")
另外,當我將鼠標懸停在test.test()
PyCharm向我顯示以下工具提示: Cannot find reference 'test' in '__init__.py'
。
我的問題是:為什么我可以import test
並運行功能test()
但不能在markov.py
import solver
?
在lib中添加一個文件(__init__.py空文件),
lib/
__init__.py
markov.py
solver.py
test.py
最終解決方案:
import sys
sys.path.append("/path/to/lib")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.