python – Import file from parent directory?

python – Import file from parent directory?

If youd like your script to be more portable, consider finding the parent directory automatically:

import os, sys
# import ../
import db

You must add the application dir to your path:

import sys
from app import object

Or from shell:

setenv PATH $PATH:path/to/dir

In case you use windows:
Adding variable to path in windows.

Or from the command line:

set PATH=%PATH%;C:pathtodir

python – Import file from parent directory?

First of all you need to make your directories into packages, by adding files:


Then you should make sure that the directory above application is on sys.path. There are many ways to do that, like making the application infto a package and installing it, or just executing things in the right folder etc.

Then your imports will work.

Leave a Reply

Your email address will not be published. Required fields are marked *