Close Menu
  • Business
    • Fintechzoom
    • Finance
  • Software
  • Gaming
    • Cross Platform
  • Streaming
    • Movie Streaming Sites
    • Anime Streaming Sites
    • Manga Sites
    • Sports Streaming Sites
    • Torrents & Proxies
  • Error Guides
    • How To
  • News
    • Blog
  • More
    • What’s that charge
What's Hot

8 Easy Ways to Fix the “Aw, Snap!” Error in Google Chrome

May 8, 2025

Does Apple TV Offer a Web Browser Application?

May 8, 2025

Why Is Roblox Not Working Right Now?

May 8, 2025
Facebook X (Twitter) Instagram
  • Home
  • About Us
  • Privacy Policy
  • Write For Us
  • Editorial Guidelines
  • Meet Our Team
  • Contact Us
Facebook X (Twitter) Pinterest
Digital Edge
  • Business
    • Fintechzoom
    • Finance
  • Software
  • Gaming
    • Cross Platform
  • Streaming
    • Movie Streaming Sites
    • Anime Streaming Sites
    • Manga Sites
    • Sports Streaming Sites
    • Torrents & Proxies
  • Error Guides
    • How To
  • News
    • Blog
  • More
    • What’s that charge
Digital Edge
Home»Software»Fix ImportError: Attempted Relative Import with No Known Parent Package Error in Python
Software

Fix ImportError: Attempted Relative Import with No Known Parent Package Error in Python

Michael JenningsBy Michael JenningsApr 28, 2023Updated:Feb 28, 2025No Comments3 Mins Read

If you’re new to Python, you may encounter an error like “ImportError: attempted relative import with no known parent package.”

This issue typically occurs when you attempt to use relative imports to access a module from an upper directory. In this article, we’ll demonstrate various methods to fix this issue, even if you’re a tech beginner. Let’s get started!

Contents hide
What Causes this Error?
How to Solve this Error?
Option 1: Get Rid of “from”
Option 2: Make an Actual Package
Option 3: Changing the Caller Program (runcode.py)
FAQs:
What is the meaning of “ImportError: attempted relative import with no known parent package” error?
How can I fix the “ImportError: attempted relative import with no known parent package” error?
Can I use relative imports to import a module from a parent folder?

What Causes this Error?

A Reddit post explains that the error “ImportError: attempted relative import with no known parent package” indicates there is no parent package for your “test” folder.

This means you cannot use relative imports to bring in something from the folder above.

How to Solve this Error?

Now, let’s dig into the various solutions to fix this error.

Option 1: Get Rid of “from”

One solution that worked for someone on Reddit1 is to get rid of “from” and use an absolute import instead. For example, instead of writing:

javascript
Copy code
[from .. import my_module]

You can write:

arduino
Copy code
[import my_module]

This solution should work if you’re importing a module from a parent folder.

Option 2: Make an Actual Package

Another solution is to make an actual package instead of just having a folder with modules. To do this, you need to create a file named “setup.py” in the parent folder and add the following code:

arduino
Copy code
[from setuptools import setup, find_packages] [setup
name=’my_package’,
version=’0.1′,
packages=find_packages(),
install_requires=[],]

After that, you need to install your package by running the following command:

arduino
Copy code
python setup.py install

Then, you can use absolute imports to import your modules. For example, instead of writing:

javascript
Copy code
[from .. import my_module]

You can write:

javascript
Copy code
[from my_package import my_module]

Option 3: Changing the Caller Program (runcode.py)

If you have a “runcode.py” file in your project, you can modify it to add the parent folder to the Python path. Here’s an example:

javascript
Copy code
[import sys] [sys.path.insert(0, ‘../’)] [from my_module import my_function]

FAQs:

What is the meaning of “ImportError: attempted relative import with no known parent package” error?

This error message means that your “test” folder does not have a parent package. So, you can’t import something from the parent folder using relative imports.

How can I fix the “ImportError: attempted relative import with no known parent package” error?

There are various solutions to fix this error, such as getting rid of “from” and using absolute imports, making an actual package, or modifying the caller program.

Can I use relative imports to import a module from a parent folder?

No, you can’t. You need to use absolute imports or make an actual package.

Michael Jennings

    Michael wrote his first article for Digitaledge.org in 2015 and now calls himself a “tech cupid.” Proud owner of a weird collection of cocktail ingredients and rings, along with a fascination for AI and algorithms. He loves to write about devices that make our life easier and occasionally about movies. “Would love to witness the Zombie Apocalypse before I die.”- Michael

    Related Posts

    Geospatial Tech: Unlocking the Earth’s Potential

    May 6, 2025

    Dedicated Forex Server: Boost Your Trading Performance with Speed and Reliability

    Apr 14, 2025

    How Technology is Shaping Healthcare in 2025

    Apr 8, 2025
    Top Posts

    12 Zooqle Alternatives For Torrenting In 2025

    Jan 16, 2024

    Best Sockshare Alternatives in 2025

    Jan 2, 2024

    27 1MoviesHD Alternatives – Top Free Options That Work in 2025

    Aug 7, 2023

    17 TheWatchSeries Alternatives in 2025 [100% Working]

    Aug 6, 2023

    Is TVMuse Working? 100% Working TVMuse Alternatives And Mirror Sites In 2025

    Aug 4, 2023

    23 Rainierland Alternatives In 2025 [ Sites For Free Movies]

    Aug 3, 2023

    15 Cucirca Alternatives For Online Movies in 2025

    Aug 3, 2023
    Facebook X (Twitter)
    • Home
    • About Us
    • Privacy Policy
    • Write For Us
    • Editorial Guidelines
    • Meet Our Team
    • Contact Us

    Type above and press Enter to search. Press Esc to cancel.