How-to articles, tricks, and solutions about PYTHON-IMPORT

Automatically create requirements.txt

You can use the pip freeze command to automatically generate a requirements.txt file in Python.

How can I import a module dynamically given the full path?

You can use the importlib.import_module function to import a module dynamically given the full path to the module.

How do I unload (reload) a Python module?

To unload a Python module, you can use the del statement to remove it from memory.

How to fix "Attempted relative import in non-package" even with __init__.py

The "Attempted relative import in non-package" error occurs when attempting to use a relative import within a script that is not part of a package.

How to import the class within the same directory or sub directory?

In Python, you can use the import statement to import a module or class from a file within the same directory or a subdirectory.

Import a module from a relative path

To import a module from a relative path in Python, you can use the importlib.import_module() function from the importlib module.

ImportError: No module named requests

This error message indicates that the "requests" module, which is used for making HTTP requests in Python, is not installed on your system or is not in the Python path.

Importing files from different folder

To import a module from a different folder in Python, you can use the sys.path.append() function to add the path to the folder containing the module to your Python path.

Importing modules from parent folder

In Python, you can use the sys.path.append() method to add the parent directory to the list of paths where Python looks for modules.

Relative imports in Python 3

Relative imports in Python 3 allow you to import modules or functions from other packages within your package hierarchy.