简体   繁体   中英

Python3: Why does import function from file only work first time?

I can't seem to find this question answered anywhere, so... MacBook Pro OSX Sierra, Pycharm CE, Python 3.6.0 :: Anaconda 4.3.1 (x86_64).

Hi I try to import a function from a file, and it works. Then I change the function in the file, and the import doesn't work: there is no change to the operation of the function. I del the function, then re-import from file, still doesn't work.

Example, in the file new.py

def new(inp):
   return(inp)

Then I import and call:

from new import new
new(9)
Out[249]:
9

Oh, I want to change the function in the file.

new.py changes to

  def new(inp):
     if type(inp) == str:
        this = inp + "five"

     return(this)

from new import new
new(9)
Out[250]:
9

Still just outputs the unmodified input "inp". Same deal if I

    del new
from new import new

Doesn't make a difference if I change the name of the function (!= filename).

In Python 2 this was handled by reload command, which is now not in Python 3 by default. You have to import it with

from importlib import reload

Then you will be able import new and reload(new)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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