Import from parent folder

I know this has already been asked multiple times (idk if on ask too, but on many other websides), but the problem is, none of the answers worked.

I want to import file1 into file2

Folder structure:

parent
  |
  |__file1.py   
  |   
  |__child
            |
            |__file2.py

Both folders contain a __init__.py

Thx to everyone who tries to help! :grinning:

Could you link to your Repl? The way to fix this will vary depending on how your Repl is set up.

1 Like

Link
Sorry that it is a bit messy, the folders i mean are test, log(inside test)

First, you should decide whether you want to use an absolute or relative import.

Absolute imports make python search for the path in all directories listed in sys.path (or equivalently PYTHONPATH env variable), most commonly the top level directory, or an installed package, or a standard library module.
If the parent folder is inside the top level directory, then these are absolute options:

# file2.py
import parent.file1
from parent import file1
from parent.file1 import foo

This approach is more explicit and often much clearer for simple file structures, than relative imports.

Relative imports are what they sound like: an import relative from the file you’re in. A dot . basically represents going backwards into the containing folder.

# file2.py
from .. import file1
from ..file1 import foo
# The ".." represents "parent" folder.
# The first dot is the "child" folder
# The second dot is the folder that "child" is in: the "parent" folder
3 Likes

Non os working. Not parent and not ...
Its sais No module named Parent

1 Like

Oh, I know why. You have to put disableGuessImports = true at the top of your .replit file. You might have to unhide it via the three dots and then select Show hidden files.

Still not working :confused:

1 Like

What exactly are you trying to import, and from where. Can you give me an exact location?

What error are you getting when using this?:

from .. import file1
2 Likes

Is the parent folder top level?
Try using the relative import:

from .. import file1
# or
from ..file1 import my_func

Also make sure that you have not installed any packages with the same name as any of the folders or files you are importing. Check pyproject.toml.

1 Like
attempted relative import with no know parent package

How can I give you access to see the folder structure?(Dont wanna give full write access)
Or should i send Screenshot?

A link to your Repl (not an invite link) doesn’t provide write access.

1 Like

Discord.py - Replit

Try

import sys 
sys.path.insert(0, '..')
import checklog

Here I changed checklog to just a simple print(“Hello world”) to show that it is successfully importing checklog.


1 Like


Idk how dumb I could be to keep failing :sob: :neutral_face:

@vvithershins @Firepup650 @NuclearPasta0 @Sky
I think Ill just put them in the same folder, bc it looks like I can’t get it to work :confused:

You should be able to.

import ..checklog

Should work fine


I’m sorry I see now that what I provided only works if you run from the shell and not with the run button.

I fixed this by changing sys.path.insert(0, '..') to
sys.path.insert(0, 'test')

3 Likes

What you are trying to do is weird and unpythonic. Your main.py “entrypoint” file should not be deeper than the modules you need to use.

You can import test/checklog.py from test/log/main.py, but you have to do a messy and fragile workaround using sys.path:

import sys
# Insert the directory path (is relative to the current working directory, not the run file)
sys.path.insert(0, 'test')

import checklog

What I would recommend is moving your main.py into test so that you can use normal import statements and a sensible file structure.

2 Likes