Python Calling Function from Another File

Windstorm1981

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?

arsho

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling a Python function from another file

From Dev

Python calling a function from another file

From Dev

Calling function from another Python file

From Dev

Calling a Python function from another file and using the DF of that function

From Dev

Calling Python function that requires libraries from another file

From Dev

Python calling function in an function from another function

From Dev

Python calling a function from another function in a class

From Dev

PHP calling function from another file not working

From

Calling a function from another file fails

From Dev

Calling an assembly function from another file

From Dev

Octave calling function from another m file

From Dev

Calling ViewController function from another .swift file

From Dev

Calling a function from another file to use in templates

From Dev

React Calling function from another file and useEffect

From Dev

Button Function Not calling From Another file in React

From Dev

Calling a function from another file into another function C++

From Dev

python - calling variable from another function

From Dev

Calling variable from another function in python

From Dev

Python - Calling a function from another class

From Dev

Calling another GUI from a file in python

From Dev

Calling text from another file in python

From Dev

Calling unspecified function from another file and another class

From Dev

Calling a variable from another function inside another file

From Dev

Matlab: Calling a function of a .m file from another .m file

From Dev

Calling a function from HTML to Python file

From Dev

Python: calling function from imported file

From Dev

Calling a function from an external file in Python

From Dev

Python calling function from other file

From Dev

Dynamic Function Calling From a Different File - Python

Related Related

  1. 1

    Calling a Python function from another file

  2. 2

    Python calling a function from another file

  3. 3

    Calling function from another Python file

  4. 4

    Calling a Python function from another file and using the DF of that function

  5. 5

    Calling Python function that requires libraries from another file

  6. 6

    Python calling function in an function from another function

  7. 7

    Python calling a function from another function in a class

  8. 8

    PHP calling function from another file not working

  9. 9

    Calling a function from another file fails

  10. 10

    Calling an assembly function from another file

  11. 11

    Octave calling function from another m file

  12. 12

    Calling ViewController function from another .swift file

  13. 13

    Calling a function from another file to use in templates

  14. 14

    React Calling function from another file and useEffect

  15. 15

    Button Function Not calling From Another file in React

  16. 16

    Calling a function from another file into another function C++

  17. 17

    python - calling variable from another function

  18. 18

    Calling variable from another function in python

  19. 19

    Python - Calling a function from another class

  20. 20

    Calling another GUI from a file in python

  21. 21

    Calling text from another file in python

  22. 22

    Calling unspecified function from another file and another class

  23. 23

    Calling a variable from another function inside another file

  24. 24

    Matlab: Calling a function of a .m file from another .m file

  25. 25

    Calling a function from HTML to Python file

  26. 26

    Python: calling function from imported file

  27. 27

    Calling a function from an external file in Python

  28. 28

    Python calling function from other file

  29. 29

    Dynamic Function Calling From a Different File - Python

HotTag

Archive