
The Python interpreter is the core engine of python that runs your scripts.

Python is an interpreted scripting language. If you are not in a rush and want to learn about how modules and packages work, then keep on reading! What’s a Module?

You can also get extra fancy and use the as keyword, which will let you define a new alias for your import. Util.doSomethingCool() # Main file for doing cool things With this structure, you can actually import in many different ways. If you are making a separate file to do helpful things, why not make it an actual package? It is actually much easier than you would think.Ĭhange up your file structure so that the functionality you want to split out is in a separate folder and includes an empty _init_.py file. If you want to learn about why this fixes the issue, check out Packages Explained. So your main.py would now look like this: # main.py Running main.py with this setup will result in: ImportError: attempted relative import with no known parent package Option 1: Get Rid of Fromĭon’t use from and just use import, then make sure you use the module reference when using your imported functions. This issue almost always occurs because you are using a flat file structure for your project, and you try to split the functionality into two files. Many resources on the web will have you fiddling with your sys.path but in most cases, this is a code smell that indicates you are not properly using modules and/or packages.

You can also jump to the Common Project Structures section if you just want some tips on how to structure your files to avoid issues like this one. If you want to understand the why behind these fixes, check out Packages Explained a bit further down.

In a hurry, and just want a quick fix? This section is for you. If you are going through this now or have in the past, this post is for you. ImportError - what does that even mean!? I just wanted to import another file.
