By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,241 Members | 2,116 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,241 IT Pros & Developers. It's quick & easy.

Problems with import of modules

P: n/a
I am within a directory

\doc\template\

I launch script.py

within this script.py, I like to import a module from the doc directory.

this here does not work:

form ..\..\module_name import this_one

how do I go back in the directory hierarchy to import something?

If this is not possible:

How can I modify the python search-path from within the script, thus it
contains the doc directory?

..

--
http://lazaridis.com
Jan 23 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
> How can I modify the python search-path from within the script, thus it
contains the doc directory?


Hi,

The sys.path variable is a list of strings that contains the current
module search path. You can add your own path to this list:

import sys
sys.path.append('../')

-Farshid
Jan 23 '06 #2

P: n/a
Ilias Lazaridis wrote:
I am within a directory

\doc\template\

I launch script.py

within this script.py, I like to import a module from the doc directory.

this here does not work:

form ..\..\module_name import this_one
Well, if you are in linux you can do this easily by changing your
PYTHONPATH environment variable, either by changing it explicitely or by
editing it in your .rc files to append the /doc directory.

Although I don't know specifically where this variable might be if you
are using windows, in either case(windows or linux), you can alter this
from python using sys.path

import sys
sys.path.append("/doc")

Hope that helps,

-carl
how do I go back in the directory hierarchy to import something?

If this is not possible:

How can I modify the python search-path from within the script, thus it
contains the doc directory?

.

--

Carl J. Van Arsdall
cv*********@mvista.com
Build and Release
MontaVista Software

Jan 23 '06 #3

P: n/a
the sys.path.append has done the work.

thanks.

..

--
http://lazaridis.com
Jan 27 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.