简体   繁体   English

ipython自动重载不起作用

[英]ipython autoreload doesn't work

autoreload doesn't work for me in subdirs at all. autoreload 在子目录中根本不适合我。

dir structure:目录结构:

run.ipynb
oof.py
pertussis/
    |-- __init__.py

on run.ipynb I have (running with notebook):run.ipynb我有(用笔记本运行):

from pertussis import *
check() #defined in the module

this doesn't work.这不起作用。 I tried everything.我什么都试过了。 I added the autoreload magic inside code, inside config file, everywhere.我在代码中、配置文件中、到处都添加了自动重载魔法。 I also added the folder of the module to the sys.path list.我还将模块的文件夹添加到 sys.path 列表中。 Never reloaded.从来没有重载过。 I tried reloading a regular file oof.py from the notebook, instead of the module directly.我尝试从笔记本重新加载常规文件oof.py ,而不是直接加载模块。

on oof.py I have:oof.py我有:

from pertussis import *
def check_2():
  print ("Hello")

What happend now is that check_2 was autoreloaded successfully, but check from the module still didn't reload.现在发生的是 check_2 已成功自动重新加载,但模块中的检查仍未重新加载。

Nothing seems to work, I am lost.似乎没有任何效果,我迷路了。

Sorry for the late response, I've just stumbled upon a similar problem. 对于迟到的回复感到抱歉,我偶然发现了类似的问题。

In your run.ipynb , have you tried: 在你的run.ipynb中 ,你尝试过:

import pertussis
pertussis.check()

Or 要么

%load_ext autoreload 
%autoreload 1

then 然后

%aimport pertussis
check = pertussis.check  # optional shortcut
check()

I don't believe this is an iPython issue, but is due to the nature of from imports.我不认为这是一个 iPython 问题,而是由于from import 的性质。

The following is from Learning Python Oreilly, p798 (5th ed.)以下来自Learning Python Oreilly, p798(第 5 版)

...because from copies (assigns) names when run, there's no link back to the modules where the names came from. ...因为在运行时from副本(分配)名称中,没有链接返回到名称来自的模块。 Names imported with from simply become references to objects, which happen to have been referenced by the same names in the importee when the from ran.使用from导入的名称简单地成为对对象的引用,当from运行时,这些对象恰好被导入者中的相同名称引用。

A workaround is to use import and name qualification.一种解决方法是使用import和名称限定。 For example:例如:

import pertussis
pertussis.check()

Then in oof.py :然后在oof.py

import pertussis # now must use full name qualification
def check_2():
  print ("Hello")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM