Imported functions from folder not running

What title says. Even the AI acknowledges that I supposedly have the imports formatted properly but doesn’t know what the error is and says to “check elsewhere” in the code.

main.py
defs.py

Thanks in advance.

I’ve looked up multiple Reddit and StackOverflow discussions on this and they all say to use star import. Some say to init to turn them into packages but I want to know the best way to get these functions (like getACode) to run from a star import of the Investigations folder.

:wave: Welcome @pklovegamma!

What is the exact error you are getting?

Look into the start of section 6.4, in particular 6.4.1, of The (official) Python Tutorial (it’s not very long).

2 Likes

Hello, welcome to the forums.

I see your problem and here is why you are getting it:
On line 1 of main.py, you wrote from Investigation import *. But what does this really do? Not what you think. This actually does absolutely nothing for certain reasons.
What you meant to put (probably) was:

from Investigation.defs import *

This imports everything that’s inside Investigation/defs.py. (Not anything else.)
(Note that “wildcard”, or * imports should generally be avoided because it’s prone to many bugs. The better option would be something like from Investigation.defs import getACode, or even from Investigation import defs and then you’d use defs.getACode().)

If you insist on using just one star import for your main.py for ALL files in Investigation, then you should make an __init__.py file inside your Investigations folder, then add code like this in it.

from .defs import *
...

The . before the defs makes it a relative import instead of an absolute import. This is important knowledge, so go look it up. Now you can do from Investigations import * in main.py.
In fact, you need to fix all of your imports inside your files.

# Investigation/defs.py
from .countries import *

(The __init__.py file is a special module that basically is the module of the Investigations “package” or folder.)

You may experience circular imports when moduleA imports moduleB, and moduleB imports moduleA. There are several ways to fix this, such as importing the module itself instead of the things inside it.

It seems you don’t have a good grasp on namespaces, modules, and importing. I would highly recommend learning these in more detail.

(If you have more questions, you can ask me.)

3 Likes

getACode() not defined

Have you tried what @NuclearPasta0 suggested?

Reading and implementing now

1 Like

If I wanted to import everything from that folder, is there a better way than a star import? It seems like it would be a lot to individually import every function and file. Should I even have them in a separate folder than main?

It is best practice to keep main.py as clean and minimal as possible. It’s good that the other files are separate from main.

Most of the time, one would import the individual functions.

from Investigation.defs import getACode, foo, bar, baz

Using classes may help reduce the number of functions that need to be imported. Good structured code reduces the number of functions that need to be imported from a module.
Sometimes it might be better to merge a few modules if their purpose is the same. You can also “merge” modules by having them in a package with an __init__.py that imports their stuff.

If there are a lot of functions or there are name conflicts, then one would just import the module(s). This is a perfectly good way of importing.

from Investigation import defs, countries, products
defs.getACode()

Star imports are considered bad practice but it can be slightly improved with the __all__ variable.

3 Likes

Making an init file worked. I ended up removing the stars and started using absolute imports on each individual function. Relative worked but it seems better just in case the directories change. I still don’t understand the all variable yet so I just have it defined and commented out but my current imports from folder are finally working. Thanks

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.