NameError: name 'reload' is not defined

前端 未结 4 1309
忘了有多久
忘了有多久 2020-12-08 18:22

I\'m using python 3.2.2. When I write a simple program, I meet the problem.

>>> reload(recommendations)
Traceback (most recent call last):
  File \"         


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-08 18:42

    As others have said, you need either importlib.reload(module) or at some earlier point you need to from importlib import reload. But you can hide the from importlib import reload in an initialization file. Make sure that PYTHONSTARTUP is defined in your shell. For example,

    export PYTHONSTARTUP=$HOME/python/startup.py
    

    might a reasonable line to add to your ~/.bash_profile, if your shell is bash, and depending on where you store your python files. (If you’re following these instructions, start a new terminal window at this point so that the line is executed.) Then you can put the line

    from importlib import reload
    

    in ~/python/startup.py and it will happen automatically. (Again, if you’re following along, start a new python session at this point.) This might look a bit complex just to solve this one problem, but it’s a thing you only have to do once, and then for every similar problem along the lines of “I wish python would always do this”, once you find the solution you can put it in ~/python/startup.py and forget about it.

提交回复
热议问题