1

How to import modules from different folders? I have the following

cgi-bin
 | py
 |
 __init.py__
 http
 |
 __init.py__
 HttpFormParser.py
 xml
 |
 __init.py__
 XmlDocumentCreator.py

I want to import XmlDocumentCreator in HttpFormParser.py. How to do that ?

I'm doing

import py.xml.XmlDocumentCreator

in HttpFormParser.py and its throwing the following error.

/HttpFormParser.py", line 5, in <module> 
 import py.xml.XmlDocumentCreator
ImportError: No module named py.xml.XmlDocumentCreator
asked May 25, 2011 at 22:53
1

2 Answers 2

1

Just work out the directory you're executing from, as told by sys.argv[0]. In this case, you want to get the cgi-bin directory since it contains py:

import os
import sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), 
 os.pardir, os.pardir)))
import py.xml.XmlDocumentCreator

P.S. if you're using WSGI, you don't need your Python code in a cgi-bin directory, where it could be downloaded if your .htaccess (or equivalent) is set wrong.

answered May 25, 2011 at 23:00
Sign up to request clarification or add additional context in comments.

Comments

0

Did you try:

from ..xml import XmlDocumentCreator

?

Generally I encourage you to read sections The Module Search Path and Intra-package References @ http://docs.python.org/tutorial/modules.html

answered May 25, 2011 at 23:02

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.