Problems with import of modules

I

Ilias Lazaridis

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?

..
 
F

Farshid Lashkari

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
 
C

Carl J. Van Arsdall

Ilias said:
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
(e-mail address removed)
Build and Release
MontaVista Software
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,282
Messages
2,571,404
Members
48,096
Latest member
Kenkian2628

Latest Threads

Top