[英]Autoreload of python file in specific path
我有一個帶有基本功能的hello.py
文件
def say_hello()
print("hello world")
return
和notebook.ipynb
在同一目錄中 (使“導入”工作)
%load_ext autoreload
%autoreload 2
import hello
然后每次我更新hello.py文件時,autoreload工作:
hello.say_hello()
Out >>> "Hello world with autoreload working"
當hello.py
文件不在同一目錄中時,我不能使用“import”,所以我有:
%load_ext autoreload
%autoreload 2
from importlib.machinery import SourceFileLoader
hello = SourceFileLoader("hello", '/path/to/hello.py').load_module()
起初它起作用:
hello.say_hello()
Out >>> "Hello world"
但是在我編輯python文件之后:
def say_hello()
print("hello world again")
return
並重新執行該函數,它返回文件的先前版本並拋出錯誤
hello.say_hello()
Out >>> "Hello world"
[autoreload of failed: Traceback (most recent call last):
File "C:\Program Files\Python36\Lib\site-
packages\IPython\extensions\autoreload.py", line 244, in check
superreload(m, reload, self.old_objects)
File "C:\Program Files\Python36\Lib\site-packages\IPython\extensions\autoreload.py", line 376, in superreload
module = reload(module)
File "C:\Program Files\Python36\Lib\imp.py", line 314, in reload
return importlib.reload(module)
File "C:\Program Files\Python36\Lib\importlib\__init__.py", line 166, in reload
_bootstrap._exec(spec, module)
File "<frozen importlib._bootstrap>", line 589, in _exec
AttributeError: 'NoneType' object has no attribute 'name'
]
如何從Jupyter筆記本中自動加載特定路徑中的Python文件?
這是一個ImportError
python版本 - 3.7。
如果找不到模塊的規范,請使用importlib.reload()
引發ImportError。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.