Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use relative imports #167

Closed
giampaolo opened this issue May 23, 2014 · 4 comments
Closed

use relative imports #167

giampaolo opened this issue May 23, 2014 · 4 comments

Comments

@giampaolo
Copy link
Owner

From [email protected] on May 30, 2011 05:23:23

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: http://code.google.com/p/psutil/issues/detail?id=167

@giampaolo
Copy link
Owner Author

From g.rodola on May 30, 2011 09:35:21

"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.

@giampaolo
Copy link
Owner Author

From [email protected] on May 30, 2011 10:08:20

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.

@giampaolo
Copy link
Owner Author

From g.rodola on May 30, 2011 10:34:53

> 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"],
      ...
     )

Status: WontFix
Labels: -Type-Defect Type-Enhancement Portability

@giampaolo
Copy link
Owner Author

From [email protected] on May 30, 2011 10:40:35

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant