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: Convert Date to DateTime
- How to sort a List using Lambda in Python
- Python matplotlib segmentation fault: 11 macOS Big Sur
- What is Terminal Velocity and its Formula? How to calculate it programmatically?
- How to install Python 3.11 on Mac
- How to flatten a nested list in Python
- Python: Pandas Merge DataFrames on Index Example
- How to Run all Cells at Once Jupyter Notebook
- Python - Convert float to String
- How to add borders to tkinter label text
- How to Exit a Loop in Python Code
- [Python] Fix: ValueError: All arrays must be of the same length
- Sorting an array using Bubble Sort in Python Programming
- How to Unzip a file using Python
- Python: Merge DataFrames Pandas Outer Join Example
- Change label (text) color in tkinter
- Convert Float to String in Python
- Fix: fatal error: No such file or directory compilation terminated
- Python: Access index/counter of a for loop iteration
- Import Other Python Files Examples
- How to install Anaconda on Mac (M1/M2 Mac)
- Python Regular Expression to Find All Matches in List
- How to Read a binary File with Python
- How to disable warnings while Python file execution
- Know current Python Version
More Posts:
- How to Enable or Disable Dark Mode on macOS Ventura 13 - MacOS
- How to Compare two Files in Bash Shell - Bash
- ChatGPT Outage: Hmm...something seems to have gone wrong. Maybe try me again in a little bit. - HowTos
- What is BTS? What is BTS A.R.M.Y? What is its meaning? - BTS
- Mac Terminal Find Command History - MacOS
- [fix] URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs) IntelliJ - Java
- [Tutorial] Install Python on Visual Studio Code (VS Code) - Python
- Setting up RSS feeds notifications within Outlook - HowTos