SilverWebBuzz

 Join Silver Webbuzz at GITEX Global 2023 – The Year to Imagine AI in Everything. Meet us.

How to Solve Importerror: attempted relative import with no known parent package?

Get in Touch With Us
Submitting the form below will ensure a prompt response from us.

    The error ImportError: attempted relative import with no known parent package usually occurs when you are using a relative import in a script that is not being executed as part of a package. In Python, relative imports only work when a module is executed as part of a package, not as a standalone script.
    To fix this error:
    Ensure you’re running the script as part of a package: Make sure you are running the script in a way that Python recognizes it as part of a package. For instance, instead of running a file directly like:
    python myscript.py
    Run the package as a module:
    python -m mypackage.myscript
    Use absolute imports: If relative imports are not essential, you can replace them with absolute imports. For example, instead of:
    from .module import something
    Use:
    from mypackage.module import something
    Adjust your folder structure: If you’re trying to use relative imports in a script inside a folder, ensure the folder is structured as a package, which means it should contain an __init__.py file.

    About Author

    Bhavik Koradiya is the CEO / Co. Founder of Silver WebBuzz Pvt. Ltd. Having 18+ years Experience in LAMP technology. I have expert in Magento, Joomla, WordPress, Opencart, e-commerce and many other open source. Specialties: Magento, WordPress, OpenCart, Joomla, JQuery, Any Open source.

    Scroll to Top