views:

434

answers:

6

I have some python modules in a shared folder on a Windows machine.

The file is \mtl12366150\test\mymodule.py

os.path.exists tells me this path is valid.

I appended to sys.path the folder \mtl12366150\test (and os.path.exists tells me this path is valid).

When I try to import mymodule I get an error saying the module doesn't exist.

Is there a way to import module that are located in shared path?

A: 

To import a python item there needs to be a __init__.py file in each of the folder above it to show that it is a valid python package.

The __init__.py files can be empty, they are there just to show structure.

\mtl12366150
  __init__.py
  \test
    __init__.py
      \mymodule.py
AutomatedTester
This is not true. If you do sys.path.append("mtl12366150/test"), those directories do not need to be packages.
bstpierre
He's importing a single file directly, not a package directory. __init__ isn't relevant.
Glenn Maynard
+1  A: 

Did you forget to use a raw string, or escape the backslashes, in your additional sys.path component? Remember that "\t" is a tab, whereas r"\t" or "\t" are a backslash followed by a tab.

In most applications you are actually better off using forward slashes rather than backslashes even for Windows paths, and most Windows APIs will accept them just fine. Otherwise, be careful to use raw strings!

[There is no need to add __init__.py files in the directories above a simple Python module]

A: 

"I appended to sys.path ..."

Please don't.

Set the PYTHONPATH environment variable from outside your application.

S.Lott
No, there are perfectly valid reasons to append to sys.path. Just don't *prepend* the path, since then you'll override any PYTHONPATH set by the user; those should take precedence. In any case, this is irrelevant to the question.
Glenn Maynard
@Glenn Maynard: "irrelevant to the question" perhaps not. Perhaps the attempted update to sys.path **is** the problem.
S.Lott
+1  A: 

You say os.path.exists() says the path is there, but are you absolutely sure you escaped the \? Try this:

sys.path.append('\\mtl12366150\\tes')
Nelson
If you really want to use backslashes, just use r"\mtl12366150\test". The better advice is to use forward slashes, of course.
Glenn Maynard
A: 

As S.Lott said, the best approach is to set the PYTHONPATH environment variable. I don't have a windows box handy, but it would look something like this from your command prompt:

c:> SET PYTHONPATH=c:\mtl12366150\test
c:> python
>>> import mymodule
>>>
bstpierre
Wouldn't you want to append to `PYTHONPATH` rather than overwrite it all?
Evan Fosmark
@Evan Fosmark: Depends. 80% of the time, it's not used. Most packages are installed in lib/site-packages. It's rare, actually, that there's any pre-existing, system-wide PYTHONPATH. Rather than set a system-wide path, we generally just install the package.
S.Lott
A: 

I think I found the answer. I was using Python 2.6.1 and with Python 2.6.2 it now works. I had the same faulty behavior with python 2.5.4.