title: How to import another python script (.py) into main python file
body: <p>is there a way to import multiple python files into a main python file?</p>
  
  <p>I have a bunch of py files and each one has to run in the main python file and the data are saved into a json file. </p>
  
  <p>This is what I tried and it gave me an error.</p>
  
  <pre><code>import light.py as light 
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>Traceback (most recent call last):
  File "&lt;frozen importlib._bootstrap&gt;", line 2218, in  _find_and_load_unlocked
  AttributeError: 'module' object has no attribute '__path__'
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
  File "/home/pi/Desktop/majorproject/pillar1.py", line 8, in &lt;module&gt;
  import sensorkey.py as sensorkey
  ImportError: No module named 'sensorkey.py'; 'sensorkey' is not a package
  </code></pre>
  
  <p>I have also tried specifying the path to the py file and it didn't work either and keeps giving an invalid syntax error. </p>
  
  <pre><code>import /home/pi/Desktop/json/light.py as light
  </code></pre>
  
  <p>Update: 
  I managed to fix the import error but i now, after importing this light.py file, i have to print out certain keys from a dictionary (key) into this new file then export it to a json file. I'm currently using TinyDB to do so. Here are my codes: </p>
  
  <pre><code>from tinydb import TinyDB, Query
  import json
  from light import key
  
  with open("/home/pi/Desktop/json/sensortestest.json", 'w+'):
      db = TinyDB('/home/pi/Desktop/json/sensortestest.json')
      table = db.table('Light')
      db.insert_multiple([{'Key 1' :key[lightkey]}, {'Key 2' : key[lightkeyID]}])
  </code></pre>
  
  <p>Error: </p>
  
  <pre><code>Traceback (most recent call last):
    File "/home/pi/Desktop/majorproject/testertestest.py", line 12, in &lt;module&gt;
      db.insert_multiple([{'Key 1' :key[lightkey]}, {'Key 2' : key[lightkeyID]}])
  </code></pre>
  
  <p>NameError: name 'lightkey' is not defined</p>
  
  <p>The problem is I had already defined 'lightkey'  in its own file already.  </p>