Posted By

chombee on 01/15/09


Versions (?)

Import a file from a directory

 / Published in: Python

When you want to import a python file but that file is not in the same directory as the python file you're importing from (e.g. it's in a subdir). You need to add the directory that contains the file you want to import to your path environment variable sys.path. sys.path contains all the places python will look for a file when you do an import.

  1. import sys
  2. sys.path.append('./markdown-1.7')
  3. from markdown import Markdown

Report this snippet  


RSS Icon Subscribe to comments
Posted By: rob_cowie on January 24, 2009

If you want to ensure that the module you are targeting is imported and not any other module on the PYTHONPATH with the same name, add the directory to the start of the sys.path list with

sys.path.insert(0, './markdown-1.7')

You need to login to post a comment.