7

More specifically let's say I have a number of .py files, with main.py importing stuff like os, pygame, math and all my other .py files, mymodule01.py etc.

My problem is that whenever main.py calls on one of my .py files and that file contains something like an os.listdir() I keep getting an error saying stuff like 'os is not defined'.

Should I just import all the required modules in each .py file I write, or is there a better way, like a centralized import that every file can recognize? With pygame especially this would be very confusing since I'd have to init pygame in each file just to use it's functions, not to mention if I want to blit something on the screen.

The python modules and packages documentation didn't help much, that or I'm really slow, also considering that after following the doc I keep getting a not found error after adding e.g. import mymodule01.py in the init.py file in the containing folder.

1
  • 1
    +1 a worthy question. To amplify a point in the selected answer, import can be thought of as giving access to another namespace; that it loads the code (only once per interpreter) can be thought of almost as a side-effect. Commented Dec 3, 2011 at 20:21

2 Answers 2

7

I think you may be under the impression that "import" acts like "include" in other languages. It doesn't.

Each module object is a singleton. There's no performance degradation or danger of initializing a module's code more than once.

Furthermore, each file has its own scope, so in your example if you define foo = 1 in main.py, foo won't be visible in mymodule01.py. You would have to import main; main.foo to see it (not that you should)

You grumble, but this is a much better system than include

Sign up to request clarification or add additional context in comments.

1 Comment

I was pretty sure that I was approaching this with the wrong mindset and it's true that I had the fuzzy impression that import is equivalent to initializing the module's code each time it is called. Thanks for clearing that up.
5

Should I just import all the required modules in each .py file I write

Yes.

With pygame especially this would be very confusing since I'd have to init pygame in each file just to use it's functions

No, only init it once. There's only one copy of the module.

3 Comments

So I assume that when I want to blit a surface I have to do this in the module where I init pygame, and whatever surfaces I come up with in some other module I have to pass them to the init module?
Not at all; all of your little modules share the same pygame module, and it doesn't matter where it was inited. You only need to import it in each module that uses it to bind the name "pygame" locally to the module. Once the init is done everything else with pygame should work fine wherever the calls happen (although you may want to think about a MVC-ish design).
Thank you, it's not as bad as I feared then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.