This has been asked 1000 times before, but nothing seems to work for me.
Using Python3.9 and VScode, though I have tested it making edits with just the plain TextEdit app on mac as well.
I cloned this repository: https://github.com/trevorbayless/cli-chess, and I would like to customize it.
I run it via the terminal: $ python3 …/__main__.py
everything works. below is the contents of __main__.py
from cli_chess.core.main import MainModel, MainPresenter
def main() -> None:
"""Main entry point"""
MainPresenter(MainModel()).run()
if __name__ == "__main__":
main()
I open the project in VScode, make a change to a file in the source code, and the change is not reflected. Only changes to __main__.py are reflected. I can move __main__.py to another directory and run the program without issue. I can even delete every project file other than __main__.py from my computer, and still run the program as if I didn’t touch a thing.
This still doesn’t make a lot of sense to me, but from what I understand, this is due to how modules are handled in Python, and I need to reload them. I can’t get this to work.
I’ve tried using importlib like below, and other variations. Nothing seems to make a difference.
import cli_chess.core.main
import importlib
importlib.reload(cli_chess.core.main)
from cli_chess.core.main import MainModel, MainPresenter
def main() -> None:
"""Main entry point"""
MainPresenter(MainModel()).run()
if __name__ == "__main__":
main()
I’ve also seen these commands floating around:
%reload_ext autoreload
%autoreload 2
I don’t understand how to utilize this. "Just add it to your code" gives me syntax errors.
Other folks have suggested restarting VScode. I quit, I reopen, nothing changes. I’m surely missing something obvious, right? It’s kind of blowing my mind how difficult it is to throw some print statements into a fairly basic python project.
Props to trevorbayless by the way, very cool chess app.
2
Answers
I solved this by finding where the module actually lives on my computer, and editing that instead of the source code in the project. The import statement in main.py was never pointing to the files in the project folder
My situation was probably overly convoluted. I downloaded and tested a 'release' before downloading the source code. My thinking is that the original executable from the release put the cli-chess module in my python site-packages, and the source code I downloaded later referenced that by default, instead of itself.
I used this code to find the path to the module on my computer, modules are in in the site-packages directory.
So I think the reloading probably worked, I was just editing the wrong files. Thanks all
To "reload" a module, you can just delete the module object and then import it again. Like this:
Although I don’t really suggest it. There’s probably a better solution to fix your problem, but if you really wanted to "reload" a module, this is a possible solution.