Import function from other file
Assuming we have a file named my_module.py
that contains a function named my_function
:
# File: my_module.py
def my_function():
print("Hello from my_function!")
We can import this function into another file using the import
statement. Here's an example:
# File: main.py
from my_module import my_function
# Call imported function
my_function() # Output: Hello from my_function!
Import function from sub directory
```python
import sys
sys.path.append('./Basics/')
from Basics import calculator
calculator.demo(name="hado")
```
Import function from other directory
```python
import sys
sys.path.append('./Basics')
import calculator
calculator.giai_phuong_trinh_bac_2(1,1,1)
```
Last updated