1

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.

0

3 Answers 3

3
  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.

Sign up to request clarification or add additional context in comments.

Comments

1
from test import println

println()

or you can call println through test module object:

test.println()

Comments

1

use from printline import printline then use it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.