new sys.path in python
Question: I am having difficulties importing a script from a directory that is not
stored in sys.path. I have a script saved as test.py in a directory called
"Development" and am trying to add the development directory to sys.path so I
can import a function from my current script called index.py.

**Here is my code for index.py:**

    
    
    import sys
    sys.path.append ('/Users/master/Documents/Development/')
    import test
    
    printline()
    

**printline() is defined in test.py as:**

    
    
    def printline():
        print "I am working"
    

**Here is the error I am receiving:**

    
    
    Traceback (most recent call last):
      File "/Users/master/Documents/index.py", line 6, in <module>
        printline()
    NameError: name 'printline' is not defined
    

Any ideas on how I can get this to work?

Thanks.


Answer:   1. If you do `import test`, the function you defined is imported into its own namespace, so you must refer to it as `test.printline()`.

  2. `test` may be the name of another module in your Python path, and since the directory you insert is appended to the path, it will be considered only if `test` is nowhere else to be found. Try inserting the path to the head of `sys.path` instead:
    
        sys.path.insert(0, "...")
    

In a vanilla Python, the culprit is likely #1, but if you do not want your
scripts to break in the future, you should also get used to #2.

