This is more of a convenience than a real problem, but the project I’m working on has a lot of separate files, and I want to basically be able to run any of those files (that all basically only contain classes) to run the main file.
Now in the middle of writing the first sentence of this question, I tried just importing main.py into each file, and that seemed to work fine and dandy, but I cant help but feeling that:
- it might cause problems, and
- that I had problems with circular imports before and I am somewhat surprised that nothing came up.
First let me say: this is most likely a bad idea, and it’s definitely not at all standard. It will likely lead to confusion and frustration down the road.
However, if you really want to do it, you can put:
Which, assuming
mypackage.main.run()is your main entry point, will let you run any file you want as if it were the main file.You may still hit issues issues with circular imports, and those will be completely unavoidable, unless
mypackage.maindoesn’t import anything… Which would make it fairly useless 🙂