nikademus79 / psutil

Automatically exported from code.google.com/p/psutil
Other
0 stars 0 forks source link

use relative imports #167

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Using version "0.2.1" one cannot use psutil as a submodule because of the way 
imports are written.  For example psutil._common imports namedtuple like so: 
from psutil._compat import namedtuple

If it used from ._compat import namedtuple, then psutil could be packaged as a 
submodule within code that uses it instead of being required explicitly on the 
python path.

I think all imports should be rewritten as relative imports.  This would make 
psutil more valuable.

Original issue reported on code.google.com by wgordo...@gmail.com on 30 May 2011 at 3:23

GoogleCodeExporter commented 8 years ago
"from . import ..." imports are supported starting from python 2.5.
Since we support python 2.4 this is not gonna happen.

Also, I'm not sure how exactly you intend to (sub) package psutil.
It generally sounds like a bad idea, assuming it is even possible in practice 
in the first place.

Original comment by g.rodola on 30 May 2011 at 4:35

GoogleCodeExporter commented 8 years ago
You could use a try block to use relative imports and fall back to the way it 
works for python 2.4 I guess.  I didn't think about 2.4 support.

Maybe I used the wrong terminology... here is an example using directory 
structure:
my_app/
    foo/
        __init__.py
    psutil/
        __init__.py
    bar/
        __init__.py 
    __init__.py

I don't see why that is a "bad" idea.  It is commonly done... and it would 
prevent requiring psutil to be installed on the pythonpath which is very useful.

Original comment by wgordo...@gmail.com on 30 May 2011 at 5:08

GoogleCodeExporter commented 8 years ago
> You could use a try block to use relative imports and fall back 
> to the way it works for python 2.4 I guess.

No, you can't, the same way you can't use b"", print() or "with" statement 
syntax on python versions < 2.6.
On python 2.4 and 2.5 all those statements fail with a SyntaxError exception.

> I don't see why that is a "bad" idea.  It is commonly done
> and it would prevent requiring psutil to be installed on 
> the pythonpath which is very useful.

It is usually done if:

- the module consists of a single py file
- the module does not contain C extensions

psutil consists of different py files and also C extension modules so you need 
to install it first, regardless of whether absolute or relative imports are 
used in py files.
A common way to solve this is problem is to use setuptools in your app's 
setup.py, as such:

from setuptools import setup

setup(...
      requires=["psutil"],
      ...
     )

Original comment by g.rodola on 30 May 2011 at 5:34

GoogleCodeExporter commented 8 years ago
Okay.  Good to know.  I never used python 2.4 and didn't realize this project 
supported it.  Thanks for the explanation... I'll go the setuptools route for 
this one.

Original comment by wgordo...@gmail.com on 30 May 2011 at 5:40