[Tutor] Best solution to modifying code within a distributed library

Mats Wichmann mats at wichmann.us
Thu Sep 20 18:14:39 EDT 2018


On 09/19/2018 09:59 PM, Chip Wachob wrote:
> Mats,
> 
> Silly question here..
> 
> But after using the git clone command, I've got a directory of the
> Adafruit project in the same directory as my project.
> 
> When I import the library, will I get the 'installed' library, or do I
> get the library that is in the project directory?
> 
> If I have to specify which library to use, how is that done?

you look at, and possibly modify, sys.path


  >>> import sys
  >>> sys.path
  ['', '/usr/lib64/python36.zip', '/usr/lib64/python3.6',
'/usr/lib64/python3.6/lib-dynload',
'/usr/lib64/python3.6/site-packages', '/usr/lib/python3.6/site-packages']


the first element '' is the local directory, so with my sys.path, it
would pick the local one first.


if you wanted the opposite, that is be _sure_ you get the installed one,
you could use a stanza something like this:


   savepath = sys.path
   sys.path = [path for path in sys.path if path.strip('.')]
   import foo
   sys.path = savepath


but this is actually kind of tricky stuff, trying to deal with possibly
two modules of the same name, so tread carefully.




More information about the Tutor mailing list