[py-dev] Local import

holger krekel holger at merlinux.eu
Wed Dec 23 16:35:50 CET 2009


On Wed, Dec 23, 2009 at 15:00 +0000, Joan Miller wrote:
> That function would be used from each test file. So supposing that
> were added to i.e. py.path, then you have to add in each test file:
> 
> import py
> py.path.import_local()
> 
> And then you can access to you source files without installing them.
> 
> Here is more clear about how is used:
> http://bitbucket.org/ares/scripy/src/f4dcdf53cba6/test/test_scripy/test_system.py
> 
> It lets import the packages under 'lib':
> http://bitbucket.org/ares/scripy/src/f4dcdf53cba6/lib/
> 
> The great advantage is that you have not to run 'python setup.py
> develop' every time that you change your source files so it's very
> helpfull during development and testing.

thanks, i see.  I think you could as well just do it once in a conftest.py file
and avoid the dynamic discovery.  The Python import system caches imports so all 
subsequent modules importing "scripy" would pick up the previously imported one. 

Either way this makes it harder to run tests against the installed
version of a package - which is nice to for checking if everything 
is correctly installed, i.e. no files, data etc. missing.  

Hum, we could think about adding a call to a new pytest_addsyspath() hook 
that could be put in a conftest.py file (and you could statically add ../lib 
to sys.path in your project) - and also introduce an option "--no-addsyspath" to 
prevent py.test from calling this hook.  This should work and help
projects to make things properly available during development but also 
run tests against an installed package. 

Makes sense to you? 

holger

 
> 
> 2009/12/23 holger krekel <holger at merlinux.eu>:
> > On Wed, Dec 23, 2009 at 13:28 +0000, Joan Miller wrote:
> >> I'm using the next function to access to the source from tests without
> >> install it (python setup.py develop). It's very usefull during
> >> development so it could be added to py.
> >
> > If you use 'python setup.py develop' you should be able to
> > import your package that you are currently working on already.
> >
> > Can you post an example how and from which file you use
> > the posted function?
> >
> > cheers,
> > holger
> >
> >
> >> ------------------------
> >> import os
> >> import sys
> >>
> >> def import_local(parent='test', child='lib'):
> >>     """Inserts local library on 'sys.path'.
> >>
> >>     Goes up until the root of the project (upper of 'parent' directory)
> >>     and inserts the 'child' directory.
> >>     """
> >>     parent_path = os.path.abspath(os.path.dirname(__file__))
> >>
> >>     while parent in os.path.basename(parent_path):
> >>         parent_path = os.path.dirname(parent_path)
> >>
> >>     sys.path.insert(0, os.path.join(parent_path, child))
> >> ------------------------
> >> _______________________________________________
> >> py-dev mailing list
> >> py-dev at codespeak.net
> >> http://codespeak.net/mailman/listinfo/py-dev
> >>
> >
> > --
> >
> _______________________________________________
> py-dev mailing list
> py-dev at codespeak.net
> http://codespeak.net/mailman/listinfo/py-dev

-- 



More information about the Pytest-dev mailing list