1

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).

Vince Hall
  • 46
  • 1
  • 6
  • It looks like you're using iPython. The answers [here](https://stackoverflow.com/questions/1254370/reimport-a-module-in-python-while-interactive) might be useful. – roganjosh Sep 15 '17 at 10:55
  • `type(inp) == str` is `False` because `inp` is not `str`... – cs95 Sep 15 '17 at 10:57
  • What I'm trying to say is, pass a string if you want a string. `new('9')` – cs95 Sep 15 '17 at 10:58

1 Answers1

3

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)

MiKo
  • 199
  • 1
  • 10
  • That doesn't seem to work. What is the process? from importlib import reload from new import new ? – Vince Hall Sep 15 '17 at 15:25
  • If you `import new` (importing the whole module is in general a better idea) instead of `from new import new` it will work. I do not think, but I am not sure, that reload allows to reload specific functions – MiKo Sep 21 '17 at 09:36
  • My colleague helped me here: 1. import importlib 2. importlib.reload(new) # the file 3. new.new(input) # file.function – Vince Hall Oct 16 '17 at 10:13
  • 1
    Or indeed, as you say 1. from importlib import reload etc. Would have been helpful if I'd have named my file and function different names! – Vince Hall Oct 16 '17 at 10:19