0

So I'm making a modular program for a security system in python, but I can't access modules I've imported in main.py from other scripts.

That is, say I have main.py that imports the random module. I use import camClass to import a script containing an object class from camClass.py in the same directory. When I try to use the random module from within the class in camClass.py, it is undefined.

How do I overcome this error?

If I have to reimport the module from within camClass.py, where do I do it? In the init function? Or just at the top of the script? Thanks

Jckxxlaw
  • 391
  • 1
  • 4
  • 12
  • Your question makes it sound like you are using a separate file for every class. You should reconsider why you're doing that. It often doesn't make sense. – BrenBarn Jul 17 '16 at 18:57
  • Possible duplicate of [How does Python importing exactly work?](http://stackoverflow.com/questions/10501724/how-does-python-importing-exactly-work) – Łukasz Rogalski Jul 17 '16 at 19:03
  • Especially part about module not being imported every single time. – Łukasz Rogalski Jul 17 '16 at 19:04

1 Answers1

0

How do I overcome this error?

Every file that wants to use a certain module has to import that module.

If I have to reimport the module from within camClass.py,

Yes, you do.

where do I do it? In the init function? Or just at the top of the script?

At the top of the module is the standard place. (Modules do not have an "init function".)

BrenBarn
  • 242,874
  • 37
  • 412
  • 384