[Python-ideas] Relative Imports
Chris Barker - NOAA Federal
chris.barker at noaa.gov
Tue Nov 13 19:45:14 EST 2018
This is somewhat unpleasant to me, especially while developing something
and trying to test it quickly.
I just want to be able to use same relative imports and run single file
with `python3 test_main.py` for example.
I had the same frustration when I first tried to use relative imports.
Then I discovered setuptools’ develop mode (now pip editable install)
It is the right way to run code in packages under development.
Running files as modules every time is tiring. This is my problem.
I could not come up with a concrete solution idea yet i am thinking on it.
Open to suggestions.
Thank you all for your help!
On Fri, Nov 9, 2018 at 4:16 PM Steven D'Aprano <steve at pearwood.info> wrote:
> On Fri, Nov 09, 2018 at 03:51:46PM -0800, danish bluecheese wrote:
> > └── src
> > ├── __init__.py
> > ├── main.py
> > └── test
> > ├── __init__.py
> > └── test_main.py
> > assume the structure above. To be able to use relative imports with such
> > fundamental structure either i can go for sys.path hacks or could run as
> > module from one further level parent.
> I don't understand. From the top level of the package, running inside
> either __init__ or main, you should be able to say:
> from . import test
> from .test import test_main
> From the test subpackage, you should be able to say:
> from .. import main
> to get the src/main module, or
> from . import test_main
> to get the test/test_main module from the test/__init__ module.
> (Disclaimer: I have not actually run the above code to check that it
> works, beyond testing that its not a SyntaxError.)
> What *precisely* is the problem you are trying to solve, and your
> proposed solution?
> Python-ideas mailing list
> Python-ideas at python.org
> Code of Conduct: http://python.org/psf/codeofconduct/
Python-ideas mailing list
Python-ideas at python.org
Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas