Python Calling Function from Another File

function, python

Solution

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.

Problem

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?

Original source

Related problems