Say you have created a Python file calculator.py and you want to access this file from another Python file then follow the below steps.
Step 1: Put the file inside a folder
Create a folder say calculator and add the calculator.py file in it.
def add(a, b):
return a + b
def subtract(a, b):
return a - b
Step 2: Create __init__.py
Next, create an empty __init__.py file within the same folder.
Step 3: Create another py file.
Now let's see various ways to access the calculator.py file from main.py
Example 1: Import the entire moduleimport calculator.calculator
result = calculator.calculator.add(5, 3)
print(result)
result = calculator.calculator.subtract(8, 4)
print(result)
Example 2: Import the entire module as alias
import calculator.calculator as calc
result = calc.add(5, 3)
print(result)
result = calc.subtract(8, 4)
print(result)
Example 3: Import specific functions from the module
from calculator.calculator import add, subtract
result = add(5, 3)
print(result)
result = subtract(8, 4)
print(result)
Example 4: Import all functions from the module
from calculator.calculator import *
result = add(5, 3)
print(result)
result = subtract(8, 4)
print(result)
Files Structure
calculator/
__init__.py
calculator.py
main.py
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Python,
- Python List of Lists with Examples
- Fix: ERROR: Could not find a version that satisfies the requirement tkinter (from versions: none) ERROR: No matching distribution found for tkinter
- How to Convert Python Notebook .ipynb (JSON) to Executable .py Module
- ModuleNotFoundError: No module named qdarkstyle.colorsystem [Python]
- Python: Pandas Merge Indicator (Left, Right and Both) Example
- Fix: EOFError Exception in Python
- How to URL Decode a Query String in Python
- How to take user input from the console in a Python program
- Compare two lists in Python and return matches
- How to change the Python Default version
- Fix: KeyError: exception in Python
- How to get the Execution Time of A Python Program
- Fix: ModuleNotFoundError: No module named boto3 [Python]
- How to get unique values from a list in Python
- How to pip install Python Modules in VSCode
- Python: Fix - TypeError: NoneType object is not iterable
- How to delete a file using Python code example
- How to create a dictionary comprehension in Python
- Python: Get just the filename without extension using Path
- How to comment out a block of code in Python
- How to add two float numbers in Python
- Python: How to POST Json Data with HTTP Request
- Get MD5 Hash as Checksum of a String in Python
- Python - Convert float to String
- How to Parse XML String in Python
More Posts:
- Show Android Studio Emulator in a Separate Window - Android-Studio
- How to add animated Gif to SharePoint Online Page - SharePoint
- How to Copy all text to Clipboard in Vim - vi
- Where is Maven .m2 local repository located on Mac - HowTos
- How to Connect to AWS Windows EC2 UI Instance from M1 Mac (Updated) - HowTos
- Fix: Cannot resolve reference to bean while setting bean property Spring applicationConfig.xml - Java
- Java Stream flatmap() Examples - Java
- MySQL: How to know which Database Schema you are on in Terminal - MySQL