βΉοΈ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.4 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://sparkbyexamples.com/python/relative-imports-in-python-3/ |
| Last Crawled | 2026-03-30 00:53:53 (12 days ago) |
| First Indexed | 2023-04-01 16:22:47 (3 years ago) |
| HTTP Status Code | 200 |
| Meta Title | Relative Imports in Python 3 - Spark By {Examples} |
| Meta Description | Relative imports in Python 3 are a way of importing modules and packages that are related to the current module or package. To create reusable code and |
| Meta Canonical | null |
| Boilerpipe Text | Relative imports in Python 3 are a way of importing modules and packages that are related to the current module or package. To create reusable code and make projects more manageable, Pythons allows you to organize code into packages and modules. To reuse code, you might need to make use of imports. In this article, we will learn relative imports in Python.
Advertisements
We will cover how to use relative imports to import modules from the same package, different packages, parent packages, and submodules.
To illustrate this topic we have the following project structure, which we will use throughout the article:
Myproject/
β
βββ main.py
β
βββ package1/
β βββ __init__.py
β βββ module1.py
β βββ module2.py
β
βββ package2/
βββ __init__.py
βββ module3.py
βββ module4.py
1. What Are Relative Imports?
Relative imports in Python 3 are a way of importing modules that are located in the same package or subpackage as the importing script. This is done by using a special syntax that indicates the relationship between the importing module and the module being imported.
Relative imports are used in place of absolute imports when you want to import a module that is located in the same package as the importing script. Relative imports are particularly useful for organizing code and avoiding naming conflicts.
2. When Should You Use Relative Imports?
In Python, there are two important ways of importing a package, module, or function. One is absolute import and the other is relative import. Both of these will work, however, there is a situation where the relative imports might be good rather than using the absolute imports.
When working with smaller packages and modules that are closely related to one another
When working with a simple package hierarchy that includes only a few levels of sub-packages
These are the two ideal cases where relative imports might be a good fit rather than using the absolute imports.
3. Relative Import Module from the Same Package
Refer to the structure of our project. Letβs say we are working in
module1.py
and we want to import
module2.py
. We can use a relative import to accomplish this.
In our case, To import
module2.py
in
module1.py
, we can use the following import statement:
# module1.py
from . import module2
3.1 Relative Import a Function
We can also use relative imports to import a specific function from a module within the same package. Suppose
module1.py
contains a function called
print_hi()
, and we want to import it in
module2.py
. We can use the following relative import statement:
# module2.py
from module2 import print_hi
print_hi()
While with absolute import we must have to mention the full path:
# Absolute import
# module1.py
from Package_1.module2 import print_hi
# Call the function
print_hi()
3.2 Relative Import of a Module
Relative imports can be used to import another module within the same package. Suppose we want to import
module1.py
in
module2.py
. We can use the following import statement:
# module2.py
from . import module1
In this case, many people are confused, when they run this code and get the error saying βattempted relative import with no known parent packageβ. This error occurs because they execute the file as a python script and not as a python module. So you need to execute this as a python module.
# Run the code on Terminal
python -m Package.module
Here
-m
stands for module.
4. Relative Import Module from Different Packages
You can use relative imports to import modules from a different package. When importing modules from a different package, we need to use a relative import that specifies the relative path of the package and module we want to import.
Referring back to the project structure, let suppose we are working in
module1.py
and we want to import
module3.py
from
package_2
. We can use a relative import to accomplish this. We need to specify the relative path of
module3.py
from
module1.py
. In this case,
module1.py
and
module3.py
are in different packages, so we need to use two periods (..) to go up one level and then specify the name of the package and module we want to import.
To import
module3.py
in
module1.py
, we can use the following relative import statement:
# module1.py
from ..package_2 import module3
Remember if you execute this is python python code is a script you will get an error, so you will need to execute it using terminal.
4.1 Relative Import a Function
Suppose
module3.py
contains a function called
bar()
, and we want to import it in
module1.py
. We can use the following relative import statement:
# module1.py
from ..package_2.module3 import bar
4.2 Relative Import a Module from a Different Sub-package
For example we want to import
module3.py
in
module2.py
. We can use the following relative import statement:
To import
module3.py
in
module1.py
, we can use the following relative import statement:
# module1.py
from ...package2 import module3
5. Relative Import from Sub Module
Importing modules from the same package, or a different package, we can also use relative imports to import modules from a sub module within the same package.
See the following project structure:
Myproject/
β
βββ main.py
β
βββ package1/
βββ __init__.py
βββ module1a.py
βββ module1b.py
βββ subpackage/
βββ __init__.py
βββ module1c.py
If we are working in
module1c.py
in
subpackage
and we want to import
module1a.py
from the same package. We can use a relative import to accomplish this. We need to specify the relative path of
module1a.py
from
module1c.py
.
In this case, we can use two periods (..) to go up one level to the parent package, and then specify the name of the module we want to import.
To import
module1a.py
in
module1c.py
, we can use the following relative import statement:
# module1c.py
from .. import module1a
6. Summary and Conclusion
In this article, we have learned how to use relative imports in python 3. We have learned how to import modules, function within the same package and different packages. I hope this article was helpful. If you have any questions, leave them in the comment section.
Happy Coding!
Related Article
Python Count Specific Character in String
Python Not Equal Operator With Examples
Python List Unpacking with Examples
Python Tuple Comparison
Negative Index of List in Python
How to find if directory exists in Python
Import Files from Different Folder in Python
Python reversed() Function with Examples
Python Add Two Lists By Index Wise
Sort using Lambda in Python |
| Markdown | [Skip to content](https://sparkbyexamples.com/python/relative-imports-in-python-3/#main)
- [Home](https://sparkbyexamples.com/)
- [About](https://sparkbyexamples.com/about-sparkbyexamples/)
\| \*\*\* Please
[**Subscribe**](https://sparkbyexamples.com/membership-account/membership-levels)
for Ad Free & Premium Content \*\*\*
[Spark By {Examples}](https://sparkbyexamples.com/)
- [Connect](https://topmate.io/naveennel)
- [\|](https://sparkbyexamples.com/python/relative-imports-in-python-3/)
- [Join for Ad Free](https://sparkbyexamples.com/membership-account/membership-levels/)
- [Courses](https://sparkbyexamples.thinkific.com/courses/)
- [Mastering Spark with Scala](https://sparkbyexamples.thinkific.com/courses/mastering-apache-spark-tutorial)
- [Mastering PySpark](https://sparkbyexamples.thinkific.com/courses/pyspark-tutorial)
- [Spark](https://sparkbyexamples.com/ "Apache Spark")
- [Spark Introduction](https://sparkbyexamples.com/)
- [Spark RDD Tutorial](https://sparkbyexamples.com/spark-rdd-tutorial/)
- [Spark SQL Functions](https://sparkbyexamples.com/spark/spark-sql-functions/)
- [Whatβs New in Spark 3.0?](https://sparkbyexamples.com/category/spark/spark-3-0/)
- [Spark Streaming](https://sparkbyexamples.com/apache-spark-streaming-tutorial/)
- [Apache Spark on AWS](https://sparkbyexamples.com/apache-spark-on-amazon-web-services/)
- [Apache Spark Interview Questions](https://sparkbyexamples.com/interview-questions/apache-spark-interview-questions/)
- [PySpark](https://sparkbyexamples.com/pyspark-tutorial/)
- [Pandas](https://sparkbyexamples.com/python-pandas-tutorial-for-beginners/)
- [R](https://sparkbyexamples.com/r-tutorial-with-examples/)
- [R Programming](https://sparkbyexamples.com/r-tutorial-with-examples/)
- [R Data Frame](https://sparkbyexamples.com/r-programming/r-data-frames/)
- [R dplyr Tutorial](https://sparkbyexamples.com/r-programming/r-dplyr-tutorial-learn-with-examples/)
- [R Vector](https://sparkbyexamples.com/r-programming/vector-in-r/)
- [Tutorials](https://sparkbyexamples.com/)
- [Hive](https://sparkbyexamples.com/apache-hive-tutorial/)
- [Snowflake](https://sparkbyexamples.com/snowflake-data-warehouse-database-tutorials/)
- [H2O.ai](https://sparkbyexamples.com/h2o-sparkling-water-tutorial-beginners/)
- [AWS](https://sparkbyexamples.com/apache-spark-on-amazon-web-services/)
- [Apache Kafka Tutorials with Examples](https://sparkbyexamples.com/apache-kafka-tutorials-with-examples/)
- [Apache Hadoop Tutorials with Examples :](https://sparkbyexamples.com/apache-hadoop-tutorials-with-examples/)
- [NumPy](https://sparkbyexamples.com/python-numpy-tutorial-for-beginners/)
- [Apache HBase](https://sparkbyexamples.com/apache-hbase-tutorial/)
- [Apache Cassandra Tutorials with Examples](https://sparkbyexamples.com/apache-cassandra-tutorials-with-examples/)
- [H2O Sparkling Water](https://sparkbyexamples.com/h2o-sparkling-water-tutorial-beginners/)
- [Pricing](https://sparkbyexamples.com/membership-account/membership-levels/)
- [Log In](https://sparkbyexamples.com/login/)
- [Toggle website search](https://sparkbyexamples.com/)
[Menu Close](https://sparkbyexamples.com/#mobile-menu-toggle)
- [Courses](https://sparkbyexamples.thinkific.com/courses/)
- [Mastering Spark with Scala](https://sparkbyexamples.thinkific.com/courses/mastering-apache-spark-tutorial)
- [Mastering PySpark](https://sparkbyexamples.thinkific.com/courses/pyspark-tutorial)
- [Spark](https://sparkbyexamples.com/ "Apache Spark")
- [Spark Introduction](https://sparkbyexamples.com/)
- [Spark RDD Tutorial](https://sparkbyexamples.com/spark-rdd-tutorial/)
- [Spark SQL Functions](https://sparkbyexamples.com/spark/spark-sql-functions/)
- [Whatβs New in Spark 3.0?](https://sparkbyexamples.com/category/spark/spark-3-0/)
- [Spark Streaming](https://sparkbyexamples.com/apache-spark-streaming-tutorial/)
- [Apache Spark on AWS](https://sparkbyexamples.com/apache-spark-on-amazon-web-services/)
- [Apache Spark Interview Questions](https://sparkbyexamples.com/interview-questions/apache-spark-interview-questions/)
- [PySpark](https://sparkbyexamples.com/pyspark-tutorial/)
- [Pandas](https://sparkbyexamples.com/python-pandas-tutorial-for-beginners/)
- [R](https://sparkbyexamples.com/r-tutorial-with-examples/)
- [R Programming](https://sparkbyexamples.com/r-tutorial-with-examples/)
- [R Data Frame](https://sparkbyexamples.com/r-programming/r-data-frames/)
- [R dplyr Tutorial](https://sparkbyexamples.com/r-programming/r-dplyr-tutorial-learn-with-examples/)
- [R Vector](https://sparkbyexamples.com/r-programming/vector-in-r/)
- [Tutorials](https://sparkbyexamples.com/)
- [Hive](https://sparkbyexamples.com/apache-hive-tutorial/)
- [Snowflake](https://sparkbyexamples.com/snowflake-data-warehouse-database-tutorials/)
- [H2O.ai](https://sparkbyexamples.com/h2o-sparkling-water-tutorial-beginners/)
- [AWS](https://sparkbyexamples.com/apache-spark-on-amazon-web-services/)
- [Apache Kafka Tutorials with Examples](https://sparkbyexamples.com/apache-kafka-tutorials-with-examples/)
- [Apache Hadoop Tutorials with Examples :](https://sparkbyexamples.com/apache-hadoop-tutorials-with-examples/)
- [NumPy](https://sparkbyexamples.com/python-numpy-tutorial-for-beginners/)
- [Apache HBase](https://sparkbyexamples.com/apache-hbase-tutorial/)
- [Apache Cassandra Tutorials with Examples](https://sparkbyexamples.com/apache-cassandra-tutorials-with-examples/)
- [H2O Sparkling Water](https://sparkbyexamples.com/h2o-sparkling-water-tutorial-beginners/)
- [Pricing](https://sparkbyexamples.com/membership-account/membership-levels/)
- [Log In](https://sparkbyexamples.com/login/)
- [Toggle website search](https://sparkbyexamples.com/)
- [Home](https://sparkbyexamples.com/)
- [About](https://sparkbyexamples.com/about-sparkbyexamples/)
# Relative Imports in Python 3
[Home](https://sparkbyexamples.com/) Β» [Python](https://sparkbyexamples.com/category/python/) Β» Relative Imports in Python 3
- Post author:[AlixaProDev](https://sparkbyexamples.com/author/haxratali0/ "Posts by AlixaProDev")
- Post category:[Python](https://sparkbyexamples.com/category/python/) / [Python Tutorial](https://sparkbyexamples.com/category/python-tutorial/)
- Post last modified:May 30, 2024
- Reading time:10 mins read

Relative imports in Python 3 are a way of importing modules and packages that are related to the current module or package. To create reusable code and make projects more manageable, Pythons allows you to organize code into packages and modules. To reuse code, you might need to make use of imports. In this article, we will learn relative imports in Python.
Advertisements
We will cover how to use relative imports to import modules from the same package, different packages, parent packages, and submodules.
To illustrate this topic we have the following project structure, which we will use throughout the article:
```
Myproject/
β
βββ main.py
β
βββ package1/
β βββ __init__.py
β βββ module1.py
β βββ module2.py
β
βββ package2/
βββ __init__.py
βββ module3.py
βββ module4.py
```
## 1\. What Are Relative Imports?
Relative imports in Python 3 are a way of importing modules that are located in the same package or subpackage as the importing script. This is done by using a special syntax that indicates the relationship between the importing module and the module being imported.
Relative imports are used in place of absolute imports when you want to import a module that is located in the same package as the importing script. Relative imports are particularly useful for organizing code and avoiding naming conflicts.
## 2\. When Should You Use Relative Imports?
In Python, there are two important ways of importing a package, module, or function. One is absolute import and the other is relative import. Both of these will work, however, there is a situation where the relative imports might be good rather than using the absolute imports.
- When working with smaller packages and modules that are closely related to one another
- When working with a simple package hierarchy that includes only a few levels of sub-packages
These are the two ideal cases where relative imports might be a good fit rather than using the absolute imports.
## 3\. Relative Import Module from the Same Package
Refer to the structure of our project. Letβs say we are working in `module1.py` and we want to import `module2.py`. We can use a relative import to accomplish this.
In our case, To import `module2.py` in `module1.py`, we can use the following import statement:
```
# module1.py
from . import module2
```
### 3\.1 Relative Import a Function
We can also use relative imports to import a specific function from a module within the same package. Suppose `module1.py` contains a function called `print_hi()`, and we want to import it in `module2.py`. We can use the following relative import statement:
```
# module2.py
from module2 import print_hi
print_hi()
```
While with absolute import we must have to mention the full path:
```
# Absolute import
# module1.py
from Package_1.module2 import print_hi
# Call the function
print_hi()
```
### 3\.2 Relative Import of a Module
Relative imports can be used to import another module within the same package. Suppose we want to import `module1.py` in `module2.py`. We can use the following import statement:
```
# module2.py
from . import module1
```
In this case, many people are confused, when they run this code and get the error saying βattempted relative import with no known parent packageβ. This error occurs because they execute the file as a python script and not as a python module. So you need to execute this as a python module.
```
# Run the code on Terminal
python -m Package.module
```
Here `-m` stands for module.
## 4\. Relative Import Module from Different Packages
You can use relative imports to import modules from a different package. When importing modules from a different package, we need to use a relative import that specifies the relative path of the package and module we want to import.
Referring back to the project structure, let suppose we are working in `module1.py` and we want to import `module3.py` from `package_2`. We can use a relative import to accomplish this. We need to specify the relative path of `module3.py` from `module1.py`. In this case, `module1.py` and `module3.py` are in different packages, so we need to use two periods (..) to go up one level and then specify the name of the package and module we want to import.
To import `module3.py` in `module1.py`, we can use the following relative import statement:
```
# module1.py
from ..package_2 import module3
```
Remember if you execute this is python python code is a script you will get an error, so you will need to execute it using terminal.
### 4\.1 Relative Import a Function
Suppose `module3.py` contains a function called `bar()`, and we want to import it in `module1.py`. We can use the following relative import statement:
```
# module1.py
from ..package_2.module3 import bar
```
### 4\.2 Relative Import a Module from a Different Sub-package
For example we want to import `module3.py` in `module2.py`. We can use the following relative import statement:
To import `module3.py` in `module1.py`, we can use the following relative import statement:
```
# module1.py
from ...package2 import module3
```
## 5\. Relative Import from Sub Module
Importing modules from the same package, or a different package, we can also use relative imports to import modules from a sub module within the same package.
See the following project structure:
```
Myproject/
β
βββ main.py
β
βββ package1/
βββ __init__.py
βββ module1a.py
βββ module1b.py
βββ subpackage/
βββ __init__.py
βββ module1c.py
```
If we are working in `module1c.py` in `subpackage` and we want to import `module1a.py` from the same package. We can use a relative import to accomplish this. We need to specify the relative path of `module1a.py` from `module1c.py`.
In this case, we can use two periods (..) to go up one level to the parent package, and then specify the name of the module we want to import.
To import `module1a.py` in `module1c.py`, we can use the following relative import statement:
```
# module1c.py
from .. import module1a
```
## 6\. Summary and Conclusion
In this article, we have learned how to use relative imports in python 3. We have learned how to import modules, function within the same package and different packages. I hope this article was helpful. If you have any questions, leave them in the comment section.
Happy Coding\!
## Related Article
- [Python Count Specific Character in String](https://sparkbyexamples.com/python/python-count-specific-character-in-string/)
- [Python Not Equal Operator With Examples](https://sparkbyexamples.com/python/python-not-equal-operator/)
- [Python List Unpacking with Examples](https://sparkbyexamples.com/python/python-list-unpacking-with-examples/)
- [Python Tuple Comparison](https://sparkbyexamples.com/python/python-tuple-comparison/)
- [Negative Index of List in Python](https://sparkbyexamples.com/python/negative-index-of-list-in-python/)
- [How to find if directory exists in Python](https://sparkbyexamples.com/python/how-to-find-if-directory-exists-in-python/)
- [Import Files from Different Folder in Python](https://sparkbyexamples.com/python/import-files-from-different-folder-in-python/)
- [Python reversed() Function with Examples](https://sparkbyexamples.com/python/python-reversed-function/)
- [Python Add Two Lists By Index Wise](https://sparkbyexamples.com/python/python-add-two-lists/)
- [Sort using Lambda in Python](https://sparkbyexamples.com/python/sort-using-lambda-in-python/)
#### LOGIN for Tutorial Menu
- [Log In](https://sparkbyexamples.com/login/)
## Top Tutorials
- [Apache Spark Tutorial](https://sparkbyexamples.com/)
- [PySpark Tutorial](https://sparkbyexamples.com/pyspark-tutorial/)
- [Python Pandas Tutorial](https://sparkbyexamples.com/python-pandas-tutorial-for-beginners/)
- [R Programming Tutorial](https://sparkbyexamples.com/r-tutorial-with-examples/)
- [Python NumPy Tutorial](https://sparkbyexamples.com/python-numpy-tutorial-for-beginners/)
- [Apache Hive Tutorial](https://sparkbyexamples.com/apache-hive-tutorial/)
- [Apache HBase Tutorial](https://sparkbyexamples.com/apache-hbase-tutorial/)
- [Apache Cassandra Tutorial](https://sparkbyexamples.com/apache-cassandra-tutorials-with-examples/)
- [Apache Kafka Tutorial](https://sparkbyexamples.com/apache-kafka-tutorials-with-examples/)
- [Snowflake Data Warehouse Tutorial](https://sparkbyexamples.com/snowflake-data-warehouse-database-tutorials/)
- [H2O Sparkling Water Tutorial](https://sparkbyexamples.com/h2o-sparkling-water-tutorial-beginners/)
## Categories
- [Apache Spark](https://sparkbyexamples.com/category/spark/)
- [PySpark](https://sparkbyexamples.com/category/pyspark/)
- [Pandas](https://sparkbyexamples.com/category/pandas/)
- [R Programming](https://sparkbyexamples.com/category/r-programming/)
- [Snowflake Database](https://sparkbyexamples.com/category/snowflake/)
- [NumPy](https://sparkbyexamples.com/category/numpy/)
- [Apache Hive](https://sparkbyexamples.com/category/apache-hive/)
- [Apache HBase](https://sparkbyexamples.com/category/hbase/)
- [Apache Kafka](https://sparkbyexamples.com/category/kafka/)
- [Apache Cassandra](https://sparkbyexamples.com/category/cassandra/)
- [H2O Sparkling Water](https://sparkbyexamples.com/category/h2o-sparkling-water/)
## Legal
- [SparkByExamples.com β Privacy Policy](https://sparkbyexamples.com/privacy-policy/)
- [Refund Policy](https://sparkbyexamples.com/refund-policy/)
- [Terms of Use](https://sparkbyexamples.com/terms-of-use/)

- Opens in a new tab
- Opens in a new tab
- Opens in a new tab
- Opens in a new tab
- Opens in a new tab
Copyright 2024 www.SparkByExamples.com. All rights reserved.
Notifications |
| Readable Markdown | Relative imports in Python 3 are a way of importing modules and packages that are related to the current module or package. To create reusable code and make projects more manageable, Pythons allows you to organize code into packages and modules. To reuse code, you might need to make use of imports. In this article, we will learn relative imports in Python.
Advertisements
We will cover how to use relative imports to import modules from the same package, different packages, parent packages, and submodules.
To illustrate this topic we have the following project structure, which we will use throughout the article:
```
Myproject/
β
βββ main.py
β
βββ package1/
β βββ __init__.py
β βββ module1.py
β βββ module2.py
β
βββ package2/
βββ __init__.py
βββ module3.py
βββ module4.py
```
## 1\. What Are Relative Imports?
Relative imports in Python 3 are a way of importing modules that are located in the same package or subpackage as the importing script. This is done by using a special syntax that indicates the relationship between the importing module and the module being imported.
Relative imports are used in place of absolute imports when you want to import a module that is located in the same package as the importing script. Relative imports are particularly useful for organizing code and avoiding naming conflicts.
## 2\. When Should You Use Relative Imports?
In Python, there are two important ways of importing a package, module, or function. One is absolute import and the other is relative import. Both of these will work, however, there is a situation where the relative imports might be good rather than using the absolute imports.
- When working with smaller packages and modules that are closely related to one another
- When working with a simple package hierarchy that includes only a few levels of sub-packages
These are the two ideal cases where relative imports might be a good fit rather than using the absolute imports.
## 3\. Relative Import Module from the Same Package
Refer to the structure of our project. Letβs say we are working in `module1.py` and we want to import `module2.py`. We can use a relative import to accomplish this.
In our case, To import `module2.py` in `module1.py`, we can use the following import statement:
```
# module1.py
from . import module2
```
### 3\.1 Relative Import a Function
We can also use relative imports to import a specific function from a module within the same package. Suppose `module1.py` contains a function called `print_hi()`, and we want to import it in `module2.py`. We can use the following relative import statement:
```
# module2.py
from module2 import print_hi
print_hi()
```
While with absolute import we must have to mention the full path:
```
# Absolute import
# module1.py
from Package_1.module2 import print_hi
# Call the function
print_hi()
```
### 3\.2 Relative Import of a Module
Relative imports can be used to import another module within the same package. Suppose we want to import `module1.py` in `module2.py`. We can use the following import statement:
```
# module2.py
from . import module1
```
In this case, many people are confused, when they run this code and get the error saying βattempted relative import with no known parent packageβ. This error occurs because they execute the file as a python script and not as a python module. So you need to execute this as a python module.
```
# Run the code on Terminal
python -m Package.module
```
Here `-m` stands for module.
## 4\. Relative Import Module from Different Packages
You can use relative imports to import modules from a different package. When importing modules from a different package, we need to use a relative import that specifies the relative path of the package and module we want to import.
Referring back to the project structure, let suppose we are working in `module1.py` and we want to import `module3.py` from `package_2`. We can use a relative import to accomplish this. We need to specify the relative path of `module3.py` from `module1.py`. In this case, `module1.py` and `module3.py` are in different packages, so we need to use two periods (..) to go up one level and then specify the name of the package and module we want to import.
To import `module3.py` in `module1.py`, we can use the following relative import statement:
```
# module1.py
from ..package_2 import module3
```
Remember if you execute this is python python code is a script you will get an error, so you will need to execute it using terminal.
### 4\.1 Relative Import a Function
Suppose `module3.py` contains a function called `bar()`, and we want to import it in `module1.py`. We can use the following relative import statement:
```
# module1.py
from ..package_2.module3 import bar
```
### 4\.2 Relative Import a Module from a Different Sub-package
For example we want to import `module3.py` in `module2.py`. We can use the following relative import statement:
To import `module3.py` in `module1.py`, we can use the following relative import statement:
```
# module1.py
from ...package2 import module3
```
## 5\. Relative Import from Sub Module
Importing modules from the same package, or a different package, we can also use relative imports to import modules from a sub module within the same package.
See the following project structure:
```
Myproject/
β
βββ main.py
β
βββ package1/
βββ __init__.py
βββ module1a.py
βββ module1b.py
βββ subpackage/
βββ __init__.py
βββ module1c.py
```
If we are working in `module1c.py` in `subpackage` and we want to import `module1a.py` from the same package. We can use a relative import to accomplish this. We need to specify the relative path of `module1a.py` from `module1c.py`.
In this case, we can use two periods (..) to go up one level to the parent package, and then specify the name of the module we want to import.
To import `module1a.py` in `module1c.py`, we can use the following relative import statement:
```
# module1c.py
from .. import module1a
```
## 6\. Summary and Conclusion
In this article, we have learned how to use relative imports in python 3. We have learned how to import modules, function within the same package and different packages. I hope this article was helpful. If you have any questions, leave them in the comment section.
Happy Coding\!
## Related Article
- [Python Count Specific Character in String](https://sparkbyexamples.com/python/python-count-specific-character-in-string/)
- [Python Not Equal Operator With Examples](https://sparkbyexamples.com/python/python-not-equal-operator/)
- [Python List Unpacking with Examples](https://sparkbyexamples.com/python/python-list-unpacking-with-examples/)
- [Python Tuple Comparison](https://sparkbyexamples.com/python/python-tuple-comparison/)
- [Negative Index of List in Python](https://sparkbyexamples.com/python/negative-index-of-list-in-python/)
- [How to find if directory exists in Python](https://sparkbyexamples.com/python/how-to-find-if-directory-exists-in-python/)
- [Import Files from Different Folder in Python](https://sparkbyexamples.com/python/import-files-from-different-folder-in-python/)
- [Python reversed() Function with Examples](https://sparkbyexamples.com/python/python-reversed-function/)
- [Python Add Two Lists By Index Wise](https://sparkbyexamples.com/python/python-add-two-lists/)
- [Sort using Lambda in Python](https://sparkbyexamples.com/python/sort-using-lambda-in-python/) |
| Shard | 46 (laksa) |
| Root Hash | 13197168827745396246 |
| Unparsed URL | com,sparkbyexamples!/python/relative-imports-in-python-3/ s443 |