2

I am using Python 2.7 on Windows 7 Professional.

I am trying to call a function saved in another file to run in this file's code.

Function called dosomething is found in anotherfile.py

anotherfile.py is in the same directory as current code.

My call in this file is simple:

import anotherfile

print anotherfile.dosomething

I am getting an error: No module named anotherfile

The problem is the same as I found in this post

I don't understand the solution but I'd like any insight?

Thank you.

EDIT: The other question/answers discuss resetting CLASSPATH and setting PYTHONPATH. I explored this but was not sure how to do this. Perhaps relevant?

2
  • 1
    anotherfile.dosomething() since it's a function Commented May 4, 2017 at 14:32
  • When I do this I get NameError: name 'anotherfile' is not defined Commented May 4, 2017 at 14:40

4 Answers 4

10

Let us have two files in the same directory. Files are called main.py and another.py.

First write a method in another.py:

def do_something():
    return "This is the do something method"

Then call the method from main.py. Here is the main.py:

import another
print another.do_something()

Run main.py and you will get output like this:

This is the do something method

N.B.: The above code is being executed using Python 2.7 in Windows 10.

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

2 Comments

Check if both files are in same directory and ensure there is no directory with the same filename
Both files are in the same directory. It is a new computer and there are no files with the same name in other directories.
6

Specify the module then the file then the import like so:

from this_module.anotherfile import dosomething

or if you want all functions from "anotherfile.py"

from this_module.anotherfile import *

and then you can call the "dosomething" command without the "anotherfile" prefix.

6 Comments

what specifically is this_module? I'm in 'this file' and have lines of code. No functions (yet)
"this_module" would refer to the app that you are working in. For example, "mysite" would be a module. (essentially, the directory)
sorry to be dense. I'm coding this up in Spyder. Would I somehow incorporate path to reference the directory?
Could you possibly post your directory structure on here for me to see?
C:\Users\MyName\Documents\Python Scripts
|
2

I ran into same problem. After ample of trials, I ended up solving it with the below mentioned solution:

Make sure your current file and anotherfile.py lies in same location of system path. Say your another.py and current file lies at location : "C:/Users/ABC" In case, one is not aware of system path. Use below code in current file:

  import sys
  print(sys.path)

  import sys
  sys.path.append('/C:/Users/ABC/')

Then you do below code in same current code:

  from another import dosomething

Comments

0

I found the issue. Python was looking in another directory for the files. I explicitly set my working directory as the path to where thisfile.py and anotherfile.py reside and it works. Thank you for all the quick replies.

1 Comment

Can you please elaborate on how you explicitly set it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.