1

I have the same Python package installed in many different locations in Mac OS. Due to specific reasons, I want to import the module from a specific installed directory.

Can anyone suggest the best way for doing this?

Brent Lamborn
  • 1,370
  • 3
  • 17
  • 37
  • 2
    Possible duplicate of [How to import a module given the full path?](http://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path) – nbryans Aug 23 '16 at 19:01
  • This is a duplicate and you should be able to find your answer already on the site. For example, see [this post](http://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path) – nbryans Aug 23 '16 at 19:01
  • 2
    Thanks nbryans. But that resource is not helpful in my case. In particular, I have two versions of package in different locations under site-packages. I am trying to import one specific version but by default python loads another version. Not sure how to import another version. I can do this using virtualenv, but in my case I want another alternative way. – jammula ganesh Aug 23 '16 at 21:28

1 Answers1

0

You can also do something like this and add the directory that the configuration file is sitting in to the Python load path, and then just do a normal import, assuming you know the name of the file in advance, in this case "config".

Messy, but it works.

configfile = '~/config.py'

import os
import sys

sys.path.append(os.path.dirname(os.path.expanduser(configfile)))

import config
nivhanin
  • 1,688
  • 3
  • 19
  • 31