Importing Modules from Parent Folder

Importing modules from parent folder

It seems that the problem is not related to the module being in a parent directory or anything like that.

You need to add the directory that contains ptdraft to PYTHONPATH

You said that import nib worked with you, that probably means that you added ptdraft itself (not its parent) to PYTHONPATH.

Cannot import modules from parent folder

All you need to do is change the second import in utils.py and make it relative to script.py's location:

utils.py

from sqlalchemy import create_engine # python package
from .lonlatboxes import lonlatboxes # my module

Import module from parent folder but still working in child directory

The correct way of doing this is to run the script with the -m switch

python -m childDirectory.childScript # from the parent of childDirectory

Then in childScript you do a simple from parentModule import runFunction.
Hacking the sys path is bad practice and using chdir should be also avoided (leads to nasty surprises)

Easiest way to import the modules from parent folder?

You can just add the directory's path to your sys.path using

import sys
sys.path.append(r'path\to\dir')

After that you can normally import the file.
You can retrieve the parent directory's path using pathlib.

Importing modules from parent folder does not work when running flask app

In order to make a package importable, it must have an (optionally empty) __init__.py file in its root. So first create an empty __init__.py file in the Utils directory.

Furthermore, you don't have to alter the import path, since you start the Flask app from the Clean_code, the simple relative importing will work. So the first three line can be removed from main.py.

Trying to import classes from parent folder as a package

If you run python test1.py and it works, that implies (unless you used some other technique to set up the sys.path) you are in the init_file_testing folder, and that folder is on the path that is searched for Python modules. If you want to from subdir import something, then you need that folder on your path - because that is the folder that contains the subdir package.

If you run python test2.py and the file is found, that implies (similar logic) you are in subdir2. Hacking the path with sys.path.append("..") does not help, because that puts the subdir folder on the path, but you need the init_file_testing folder instead. In relative terms, that is sys.path.append("../..").


However, if you are willing to use relative paths as part of a hack to make this work, then why not just use relative imports instead? This is how you are intended to work within packages.

That looks like:

test1.py

from .subdir import Apple, Banana

__init__.py

from .class1 import Apple
from .class2 import Banana

test2.py

from ..subdir import Apple, Banana

Then, you only need to ensure that the package root (init_file_testing) is on the module search path, no matter which module you start from; and that you start from either the root folder or from outside the package folders (which you should do anyway). One easy way to ensure the path is set up is by installing your package in a virtual environment (which is the recommended approach for development anyway). You can also do it with the PYTHONPATH environment variable.

Import a function from a module in another folder in parent directory

This is what worked for me.

I exported the directory of the parent directory (/parent_folder) to the PYTHONPATH.

export PYTHONPATH=$PYTHONPATH:/home/username/Desktop/parent_folder

Then, inside file module1/script1.py, i had this line changed:

from module2.script2 import subtract_numbers

Now i can call the script script1.py that calls the function declared in module2/script2.py with python script1.py and it will work.

I also have to note that i have __init__.py files everywhere (in the parent directory and both subfolders), but i am now sure if this is vital.



Related Topics



Leave a reply



Submit