title: How to import all defined functions from python script one into python script two?
body: <p>I am trying to import a python script containing several defined functions into a different python script <a href="https://stackoverflow.com/a/4116384/7345804">using this approach via SO</a>. Since the script I would like to import defines many functions (some of which are nested inside other functions), I would prefer to import the entire script rather than each function individually. I am using a simpler example below.</p>
  
  <p>Say I have a folder on my <code>desktop</code> named <code>'workspace'</code>. In the folder <code>'workspace'</code>, there is a script named <code>'pre.py'</code> that contains a single function (defined below) and a blank file named <code>'__init__.py'</code>. </p>
  
  <pre><code>def get_g(x):
      """ Sample Function """
      if isinstance(x, list):
          res = [xi**2 for xi in x]
      else:
          res = x**2
      return res
  </code></pre>
  
  <p>In the same <code>'workspace'</code> folder, there is a script named <code>'post.py'</code>, in which I would like to import all functions defined in <code>'pre.py'</code>. A sample script of <code>'post.py'</code> is below.</p>
  
  <p>ATTEMPT #1:</p>
  
  <pre><code>import matplotlib.pyplot as plt
  
  def get_my_imports(script_name, fileloc="/Users/myname/Desktop/workspace"):
      """ Import Sample Function (defined above) """
      from fileloc import script_name
  
  get_my_imports('pre.py')
  
  x = np.linspace(1, 10, 100)
  y = get_g(x)
  
  plt.plot(x, y)
  plt.show()
  
  &gt;&gt; ImportError: No module named 'fileloc'
  </code></pre>
  
  <p>ATTEMPT #2:</p>
  
  <pre><code>import matplotlib.pyplot as plt
  from "/Users/myname/Desktop/workspace" import 'tst.py' ## error in this line
  
  x = np.linspace(1, 10, 100)
  y = get_g(x)
  
  plt.plot(x, y)
  plt.show()
  
  &gt;&gt; SyntaxError: invalid syntax
  </code></pre>
  
  <p>In my first attempt, I tried to generalize the approach of importing py scripts. But the interpreter thinks <code>fileloc</code> is an importable module rather than my intention. My second attempt tells me that I am not understanding something fundamental about how to do this. How can this style of approach be adapted to work correctly? (I am using an apple laptop if it's relevant to the file location.) </p>