[Python-Dev] Relative imports in Py3k

Ron Adam rrr at ronadam.com
Mon Oct 11 20:01:06 CEST 2010



On 10/11/2010 07:27 AM, Nick Coghlan wrote:
> On Mon, Oct 11, 2010 at 1:54 AM, anatoly techtonik<techtonik at gmail.com>  wrote:
>> On Sun, Sep 26, 2010 at 2:32 PM, Nick Coghlan<ncoghlan at gmail.com>  wrote:
>>> This is almost certainly failing because the directory containing the
>>> spyderlib package isn't on sys.path anywhere (instead, whichever
>>> directory contains the script you executed directly will be in there,
>>> which will be somewhere inside the package instead of outside it). Put
>>> the appropriate directory in PYTHONPATH and these tests should start
>>> working.
>>
>> This is a hack. I use relative imports, because I don't want to care
>> about PYTHONPATH issues. I work with two clones of spyderlib
>> (reference point and feature branch). You propose to switch PYTHONPATH
>> every time I want to execute debug code in the __main__ section from
>> either of them.
>
> Anatoly, unconstructive responses like this are why people often react
> negatively to your attempts to be "helpful".
>
> I specifically mentioned 2 things you could do:
> - modify PYTHONPATH
> - use -m to execute your modules and just switch your current working
> directory depending on which version of spyderlib you want to execute

I don't recall Anatoly saying which p3k version and revision he was using. 
  Relative imports was broken for while in 3.2.  It's fixed now and I 
presume he is using a fairly current revision of 3.2.

When you do a "make install" for 3.2 on Ubuntu, the current directory path 
"", isn't perpended to sys.path.  I don't know if that is an over site or 
not, but it could be a factor.


A few more suggestions ...

Make A test runner script which modifies sys.path.  It also could be 
considered a hack, but it doesn't require modifying PYTHONPATH, so it 
wouldn't have any potential to have side effects on other modules/programs.

One of my personal choices when writing large applications (rather than 
scripts), is to make a local "lib" directory and prepend that to sys.path 
in the main application file before any local imports.

     # Add a local lib to the search path.
     lib = os.path.abspath(os.path.join(__file__, '..', 'lib'))
     sys.path.insert(0, lib)

     [Appliction dir not in PYthon path]
         main_app_file.py
         test.py
         [lib]
             [test package]
                 ...           #test modules
             ...               #other local modules and packages

I then add a -test option to the main_app_file.py or a create test.py file 
at the same level as the main_app_file.  The test runner also needs to add 
lib to sys.path, but after that it can import and find any/all tests you 
want to run.  The test modules can use relative imports as long as they 
aren't circular.

* The error message in the case of circular imports could be much better!

Cheers,
    Ron



More information about the Python-Dev mailing list