Import Script from a Parent Directory

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.

Import Script from a Parent Directory

You don't import scripts in Python you import modules. Some python modules are also scripts that you can run directly (they do some useful work at a module-level).

In general it is preferable to use absolute imports rather than relative imports.

toplevel_package/
├── __init__.py
├── moduleA.py
└── subpackage
├── __init__.py
└── moduleB.py

In moduleB:

from toplevel_package import moduleA

If you'd like to run moduleB.py as a script then make sure that parent directory for toplevel_package is in your sys.path.

Import module from parent directory

Since the main directory is the root directory of your project, you should not include main in your absolute import:

from conf import mysql

or with relative import, you can do:

from ..conf import mysql

How do i import a function from a file in a parent directory

in the python structure for import, you should use system layout I mean:

parent/
__init__.py
one/
__init__.py
two/
__init__.py
three/
__init__.py

so first of all use init file the simply use import



Related Topics



Leave a reply



Submit