NameError -- imported modules when script is broken down in multiple python files
<p>It is hard to find a title for this question and hopefully this thread is not a duplicate. </p>

<p>I was writing that long script in Python 2.7 (using PyCharm 2016.2.2) for a project and decided to split it in different .py files which I could then import into a main file. </p>

<p>Unfortunately, it seems that when importing a module (e.g. numpy) earlier in the code does not mean that the .py files that are imported below will have knowledge of that.</p>

<p>I am new to python and I was wondering whether there is an easy workaround for that one. </p>

<p>To be more clear, here is an example structure of my code:</p>

<p><i>Main.py</i> (the file that is used to run the script):</p>

<pre><code>import basic
numpy.random.seed(7)
import load_data
</code></pre>

<p><i>basic.py</i>:</p>

<pre><code>import pandas
import numpy
            etc...
</code></pre>

<p><i>load_data.py</i>:</p>

<pre><code>raw_input = pandas.read_excel('April.xls', index_col = 'DateTime') 
            etc...
</code></pre>

<p>The second line of <i>Main.py</i> would cause an error <b>"NameError: name 'numpy' is not defined"</b>, meaning the numpy that was imported in <i>basic.py</i> is not passed to the <i>Main.py</i>. </p>

<p>I guess that a similar error would occur for the code in <i>load_data.py</i> since 'pandas' would not be a defined name. </p>

<p>Any ideas?</p>

<p>Thanks.</p>

<p>The <code>numpy</code> module imported in <code>basic.py</code> has a reference defined only withing <code>basic.py</code> scope. You have to explictly import <code>numpy</code> everywhere you use it.</p>

<pre><code>import basic.py
import numpy
numpy.random.seed(7)
import load_data.py
</code></pre>

python python-2.7