Python import relative path

Code examples

6
0

import python module from another directory

import sys
# sys.path is a list of absolute path strings
sys.path.append('/path/to/application/app/folder')

import file
2
0

python import file from different directory

# By default, you can't. When importing a file, Python only 
# searches the current directory, the directory that the 
# entry-point script is running from, and sys.path which includes
# locations such as the package installation directory 
# (it's actually a little more complex than this, but this covers
# most cases).

# you can however, add to the path at runtime

import sys
# insert at position 1 in the path, as 0 is the path of this file.
sys.path.insert(1, '/path/to/application/app/folder')

import file

file.function()
2
0

import local module python

# If the module is in the same directory:
import <module_name>

# If the module isn't in the same directory:
import sys
# insert at 1, 0 is the script path (or '' in REPL)
sys.path.insert(1, '/path/to/application/app/folder')

import <module_name>
1
0

relative path python

import sys,os
sys.path.append(os.path.realpath('..'))
0
0

python how to import library absoluth path

import sys
sys.path.append('/foo/bar/my_module')
# Considering your module contains a function called my_func, you could import it:
from my_module import my_func
# Or you could import the module as a whole,
import my_module

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................
Балгарскі
..................................................................................................................
Íslensk
..................................................................................................................