Proper way to use nested modules
<p>I've split a program into three scripts. One of them, 'classes.py', is a module defining all the classes I need. Another one is a sort of setup module, call it 'setup.py', which instantiates a lot of objects from 'classes.py' (it's just a bunch of variable assignments with a few for loops, no functions or classes). It has a lot of strings and stuff I don't want to see when I'm working on the third script which is the program  itself, i.e. the script that actually does something with all of the above.</p>

<p>The only way I got this to work was to add, in the 'setup.py' script:</p>

<pre><code>from classes import *
</code></pre>

<p>This allows me to write quickly in the setup file without having the namespace added everywhere. And, in the main script:</p>

<pre><code>import setup
</code></pre>

<p>This has the advantages of PyCharm giving me full code completion for classes and methods, which is nice.</p>

<p>What I'd like to achieve is having the main script import the classes, and then run the setup script to create the objects I need, with two simple commands. But I can't import the classes script into the main script because then the setup script can't do anything, having no class definitions. Should I import the classes into both scripts, or do something else entirely?</p>

<p>Import in each file. Consider <a href="http://stackoverflow.com/questions/3106089/python-import-scope">this SO post</a>. From the answer by Mr Fooz there,</p>

<blockquote>
  <p>Each module has its own namespace. So for boo.py to see something from an external module, boo.py must import it itself.</p>
  
  <p>It is possible to write a language where namespaces are stacked the way you expect them to: this is called dynamic scoping. Some languages like the original lisp, early versions of perl, postscript, etc. do use (or support) dynamic scoping.</p>
  
  <p>Most languages use lexical scoping instead. It turns out this is a much nicer way for languages to work: this way a module can reason about how it will work based on its own code without having to worry about how it was called.</p>
  
  <p>See this article for additional details: <a href="http://en.wikipedia.org/wiki/Scope_%28programming%29" rel="nofollow">http://en.wikipedia.org/wiki/Scope_%28programming%29</a></p>
</blockquote>

<p>Intuitively this feels nicer too, as you can immediately (in the file itself) see which dependencies the code has - this will allow you to understand your code much better, a month, or even a year from now.</p>

python python-3.x