Newbie: import

G

genkuro

I thought import used relative paths from either the python executable
or the script being executed. I have a script pulling in code from an
arbitrary directory. How is this happening?

It's a RHEL 4 environment by the way. I couldn't find any relevant
environment variables.

Thanks from a newbie for any insight.
 
L

Larry Bates

I thought import used relative paths from either the python executable
or the script being executed. I have a script pulling in code from an
arbitrary directory. How is this happening?

It's a RHEL 4 environment by the way. I couldn't find any relevant
environment variables.

Thanks from a newbie for any insight.

PYTHONPATH environment variable is next in line to be searched.

-Larry
 
7

7stud

I thought import used relative paths from either the python executable
or the script being executed. I have a script pulling in code from an
arbitrary directory. How is this happening?

It's a RHEL 4 environment by the way. I couldn't find any relevant
environment variables.

Thanks from a newbie for any insight.
From the online python tutorial here:

http://docs.python.org/tut/

There is this:

----------
6.1.1 The Module Search Path

When a module named spam is imported, the interpreter searches for a
file named spam.py in the current directory, and then in the list of
directories specified by the environment variable PYTHONPATH. This has
the same syntax as the shell variable PATH, that is, a list of
directory names. When PYTHONPATH is not set, or when the file is not
found there, the search continues in an installation-dependent default
path; on Unix, this is usually .:/usr/local/lib/python.

Actually, modules are searched in the list of directories given by the
variable sys.path which is initialized from the directory containing
the input script (or the current directory), PYTHONPATH and the
installation-dependent default. This allows Python programs that know
what they're doing to modify or replace the module search path. Note
that because the directory containing the script being run is on the
search path, it is important that the script not have the same name as
a standard module, or Python will attempt to load the script as a
module when that module is imported. This will generally be an error.
See section 6.2, ``Standard Modules,'' for more information.
----------

You can use the following trick to import one of your own modules when
your module
isn't in the current directory(and doesn't happen to be in PYTHONPATH
or the default path):

import sys
sys.path.append("relative path or absolute path to dir with the
module") # temporarily changes sys.path

import mymodule
 
7

7stud

Here's an example where you are trying to import a function in one of
your .py files--when that .py file is not in the current directory:


import sys
sys.path.append("/Users/me/2testing/dir1/")
#directory that contains the file

import test1 #name of file without .py extension
test1.greet()
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top