Python Import Quick Tutorial – Modules, Packages, and Libraries Explained!

Publicado em: 14 Julho 2025
no canal de: LinuxHowTo
64
0

Need to import Python files, modules, or libraries? In this video, we’ll cover how to import your own Python files, use subfolders as packages, and import built-in and external libraries. Perfect for beginners or anyone needing a refresher on Python imports!

Download Quick Guid & Project Files:
🔗 https://gitlab.com/hatem-badawi/linux...


Learn:
✅ 1. Import Your Own Python Files (Modules)

File Structure:
Copy
project/
├── main.py
└── mymodule.py

mymodule.py:

def greet(name):
return f"Hello, {name}!"

main.py:

import mymodule
print(mymodule.greet("World"))

Or Import a Specific Function:

from mymodule import greet
print(greet("World"))

Note: Ensure both files are in the same folder or that mymodule.py is in the Python path.


✅ 2. Import from a Subfolder (Package)

File Structure:
Copy
project/
├── main.py
└── utils/
├── __init__.py # Optional in Python 3
└── tools.py

utils/tools.py:

def say_hi():
return "Hi from tools!"

main.py:

from utils.tools import say_hi
print(say_hi())

Note: In Python 3, __init__.py is optional. Use it for controlling imports, organizing larger codebases, or ensuring backward compatibility with Python 2.

✅ 3. Import Built-in Python Libraries

Example:

import math
print(math.sqrt(16)) # Output: 4.0


✅ 4. Import External Libraries (e.g., NumPy)

Install First:

pip install numpy
or use apt on Ubuntu
sudo apt install python3-numpy

Use in Python:

import numpy as np
array = np.array([1, 2, 3])
print(array.mean()) # Output: 2.0


✅ Why Use Python Imports?

Modularity: Break down your code into manageable, reusable components.
Organization: Keep related functions and classes in separate files or packages.
Reusability: Easily share and reuse code across different projects.
Efficiency: Leverage built-in and external libraries for powerful functionality.


✅ Pro Tips:

Use from module import function for clarity when using a single function.
Use aliases for large libraries (e.g., import pandas as pd).
Keep __init__.py in packages for clarity and control.
Organize related files into packages/folders for better project structure.
Avoid wildcard imports (e.g., from module import *) to prevent namespace pollution.
Use virtual environments to manage dependencies for different projects.


Perfect for Python developers who want to manage their code efficiently! Hit subscribe for more Python tips and like if this helped.
Let us know: What’s your favorite Python import trick?

👉 Watch now and master Python imports!

#PythonTips #ImportModules #Packages #Libraries #Programming

(Short, clear, and packed with practical knowledge!)


Nesta página do site você pode assistir ao vídeo on-line Python Import Quick Tutorial – Modules, Packages, and Libraries Explained! duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário LinuxHowTo 14 Julho 2025, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 64 vezes e gostou 0 espectadores. Boa visualização!