views:

653

answers:

5

I'm running PyLint from inside Wing IDE on Windows. I have a sub-directory (package) in my project and inside the package I import a module from the top level, ie.

__init__.py
myapp.py
one.py
subdir\
    __init__.py
    two.py

Inside two.py I have import one and this works fine at runtime, because the top-level directory (from which myapp.py is run) is in the Python path. However, when I run PyLint on two.py it gives me an error:

F0401: Unable to import 'one'

How do I fix this?

+3  A: 

Do you have an empty __init__.py file in both directories to let python know that the dirs are modules?

The basic outline when you are not running from within the folder (ie maybe from pylint's, though I haven't used that) is:

topdir\
  __init__.py
  functions_etc.py
  subdir\
    __init__.py
    other_functions.py

This is how the python interpreter is aware of the module without reference to the current directory, so if pylint is running from its own absolute path it will be able to access functions_etc.py as topdir.functions_etc or topdir.subdir.other_functions, provided topdir is on the PYTHONPATH.

UPDATE: If the problem is not the __init__.py file, maybe just try copying or moving your module to c:\Python26\Lib\site-packages -- that is a common place to put additional packages, and will definitely be on your pythonpath. If you know how to do Windows symbolic links or the equivalent (I don't!), you could do that instead. There are many more options here: http://docs.python.org/install/index.html, including the option of appending sys.path with the user-level directory of your development code, but in practice I usually just symbolically link my local development dir to site-packages - copying it over has the same effect.

bvmou
Yes, I do have `__init__.py` in both directories. I think the problem is that the top dir is NOT in PYTHONPATH when PyLint runs and I'm not sure how to fix that.
Evgeny
The symlink is a good idea, but it's only supported from Windows Vista onwards and I'm running XP. I suppose I could try hard-linking it...
Evgeny
re hardlinking: No, you better don't do it. I've played with it and, while it certainly works (in a way), it will not work as you expect.
shylent
Why, what happens?
Evgeny
+2  A: 

Maybe by manually appending the dir inside the PYTHONPATH?

sys.path.append(dirname)
Mario Mikić
Hmm, but where would you put that code? At the top of each and every file? I suppose it would work, but it's quite a hack just to get PyLint to work.
Evgeny
@Evgeny: See the answer that Brian M. Hunt recently posted.
JAB
A: 

One workaround that I only just discovered is to actually just run PyLint for the entire package, rather than a single file. Somehow, it manages to find imported module then.

Evgeny
+1  A: 

There are two options I'm aware of.

One, change the PYTHONPATH environment variable to include the directory above your module.

Alternatively, edit ~/.pylintrc to include the directory above your module, like this:

[General]
init-hook='import sys; sys.path.append("/path/to/root")'

Both of these options ought to work.

Hope that helps.

Brian M. Hunt
+1  A: 
JAB