Reputation: 733
I created a function in one file, and want to access it while running it in another script in python ( for example- in MatLab, you create a function as a file, and can access it in other programs )
Upvotes: 1
Views: 9651
Reputation: 257
It looks like you are in initial stage of learning python.
Basically in python, we have modules. what it is ?
Module In basic terminology, it is a python file which is a collection of functions or classes or both
Package
This package is a collection of modules where it should have __init__.py
in this so that python treats that this is a python package and provides the PYTHONPATH track for this if you set the PYTHONPATH to the root of the project
For your question, yes you can have function in one module and can be imported in another module. see the example below:
<code>
def test_one():
print("This is test one function)"
</code>
Save this above function in testone.py and create module called testtwo.py and import the above function
<code>
import testone
def test_two():
test_one()
print("after test one")
</code>
output:
<code>
This is test one function
after test one
</code>
Python is very simple. try learning and see the power of dynamic typing.
Upvotes: 5
Reputation: 155
You don't really need the location of the module. It should be somewhere in the PYTHONPATH or in the same directory. You import it by the command import and then you can use it. I suggest you read up on modules and how the import works in python: python3 import
Upvotes: 1