1

I'm working in the vscode built in terminal and calling functions written into a script I'm calling directly from. I've got my cwd in the folder and imported the script in. For some reason, the terminal can't see when I update the script, forcing me to close the terminal and open a new one in order to run the code with the changes I've made, is there a way to make it update automatically without having to refresh the terminal? I want to avoid retyping the commands to get my cwd in the right folder and importing the script every time I make a change to a function.

I'm calling the script directly from the terminal. So for example, I already have the script written and I'm in the terminal, I'm entering the commands:

>>>import sys
>>>sys.path.append('functions')
>>>import Binomials as bn

After performing these commands in the terminal, I go back into the script 'Binomials' and add a new function 'ExpectedVals', save, and then go back to the terminal and enter:

>>>bn.ExpectedVals(2, 4, .6)

To which the console responds AttributeError: module 'Binomials' has no attribute 'ExpectedValls'

Refreshing my terminal resolves the issue as it updates to see the newly added function and it works perfectly fine. However, this requires me to re-enter the first set of commands to get into the correct cwd and load the script. Is there a way to get the terminal to be able to see the updates without needing to open a new terminal?

Tanner Burton
  • 69
  • 1
  • 9
  • Are you running with like `F5` if so, what does your `.vscode/launch.json` look like? Or do you have python open in a terminal and are not seeing the updates you made to `import custom_file.py` that you might want to `importlib.reload()`? – JonSG Feb 04 '22 at 20:28
  • 1
    Does this answer your question? [How do I unload (reload) a Python module?](https://stackoverflow.com/questions/437589/how-do-i-unload-reload-a-python-module) – JonSG Feb 04 '22 at 20:34
  • I updated the question to explain more clearly @JonSG – Tanner Burton Feb 04 '22 at 21:36
  • I don't think that other question resolves it, it seems like that question is asking about reloading a server, which I'm not doing, it also appears to require importing the importlib module into the script. Which isn't what I'm looking for either. I'm trying to stay in my terminal and enable the terminal to see changes I made to the script. – Tanner Burton Feb 04 '22 at 21:42
  • You have to think about how `import` works. When you run `import`, it reads your file and creates a module object called `bn`, in this case. That's the last time it ever reads that file, even if you call `import` again. When you change the file, that has no impact on the module object already in memory. You have to do a reload to force it to be reloaded. – Tim Roberts Feb 04 '22 at 21:46
  • 1
    If you `import importlib` then in your terminal can you do `bn = importlib.reload("Binomials")` and get your latest code? – JonSG Feb 04 '22 at 21:49
  • That command gives the error ```TypeError: reload() argument must be a module```. I just needed to remove the quotations and now it works. Thanks! After a little bit of playing around, this doesn't affect deletions, so If I remove a function and reload, I can still call it, but if I edit it, it reads the edits. I supposed functionally this accomplishes what I need. But that is very strange behavior. – Tanner Burton Feb 05 '22 at 19:55

0 Answers0