JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Importing modules from a subdirectory in Python

Author:JIYIK Last Updated:2025/05/06 Views:

This tutorial will explain various ways to import modules from subdirectories in Python. Suppose we have a file in a subdirectory of our project directory and we want to import this file and use its methods in our code.

We can import files in Python by declaring a subdirectory as a package. This way Python will treat it as a package and we can import files in that directory. Another option can be to add the subdirectory to PYTHONPATHthe environment variable, which is the path where Python looks for packages to import by default. The details and usage of these methods are given below.


__init__.pyImport a module from a subdirectory in Python using

__init__.pyThe file is used to declare a directory as a package. __init__.pyThe file prevents Python from mixing up directories with the same name, so in order to distinguish a simple directory from a package, __init__.pythe file is used.

__init__.pyThe file can be empty or contain the code needed to initialize the package and a list of modules that the package needs to import, which __all__is represented by the variable.

Once __init__.pythe file is added to the subdirectory, we can import the file from the subdirectory as shown in the example code below.

import subdirectory.myfile

sys.path.insert()Import a module from a subdirectory in Python using the

Another way to import files from a subdirectory is to add that directory to PYTHONPATH. PYTHONPATHis an environment variable that contains the path to the directories that Python searches for import packages.

So if we sys.path.insert()add a subdirectory PYTHONPATHto using the method, Python will first look for PYTHONPATHthe directory in and import it from there.

sys.path.insert()The method will insert the subdirectory into the sys.pathor PYTHONPATHvariable, which contains the list of directories that Python will search to import the required modules.

The following sample code demonstrates how to use sys.path.insert()the method to import a file from a subdirectory.

import myfile
import sys

sys.path.insert(0, "./subdirectory")

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Writing logs to a file in Python

Publish Date:2025/05/06 Views:132 Category:Python

This tutorial will show you how to write logs to files in Python. Use the module in Python logging to write logs to files Logging is used to debug a program and find out what went wrong. logging The log module is used to log data to a file

Comparing two dates in Python

Publish Date:2025/05/06 Views:97 Category:Python

This tutorial explains how to compare two dates in Python. There are multiple ways to determine which date is greater, so the tutorial also lists different sample codes to illustrate the different methods. Comparing two dates in Python usin

Reload or unimport modules in Python

Publish Date:2025/05/06 Views:58 Category:Python

Modules allow us to store definitions of different functions and classes in Python files, which can then be used in other files. pandas , NumPy , scipy , Matplotlib are the most widely used modules in Python. We can also create our own modu

Pausing program execution in Python

Publish Date:2025/05/06 Views:156 Category:Python

This tutorial will demonstrate various ways to pause a program in Python. Pausing the execution of a program or application is used in different scenarios, such as when a program requires user input. We may also need to pause the program fo

If not statement in Python

Publish Date:2025/05/06 Views:123 Category:Python

The statement in Python if checks a specific condition and executes a block of code if the condition is true. if not The does the opposite of if the statement. It tests if a condition is false and then executes some statements. Using if not

Enumerating a dictionary in Python

Publish Date:2025/05/05 Views:98 Category:Python

The function in Python enumerate() returns an object of enumeration type and adds a counter variable to iterate over a list or other type of collection. It makes looping over such objects easier. When we pass an enumeration object to list()

Changing dictionary values in Python

Publish Date:2025/05/05 Views:109 Category:Python

This tutorial will discuss various ways to change the value of a particular key in Python dictionary. We can do this by using the following methods. dict.update() method for cycle. Dictionary Unpacking dict.update() How to change dictionary

Finding the maximum value in a Python dictionary

Publish Date:2025/05/05 Views:61 Category:Python

This tutorial explains how to get a key with the maximum value in Python. Since the method has changed from the previous Python versions, it also lists some sample codes to clarify the concepts. Use operator.itemgetter() the method to get t

How to read input from stdin in Python

Publish Date:2025/05/05 Views:125 Category:Python

This tutorial discussed stdin the methods of reading input from in Python. You can read directly from the console or from a file name specified in the console. In Python, fileinput.input() use stdin fileinput We can use the read module in P

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial