Dynamically importing repository after running setup.py

74 views Asked by At

The Problem

I have a Python script that runs in the background. At some point the script tries to import a module: import mymodule.

Before the import line is executed in the Python script, another bash script (successfully) installs mymodule by running python setup.py install.

The problem is that import mymodule in the Python script is not working because mymodule is not found, even though it's installed.

My solution

I checked sys.path before installing mymodule and after I saw that a new line had been added: /usr/lib/python2.7/dist-packages/mymodule-1.0py.egg. So before the line importing the module I added the line sys.path.append("/usr/lib/python2.7/dist-packages/mymodule-1.0py.egg").

My question is whether there is a better, less hardcoded way to solve the problem.

1

There are 1 answers

0
IberĂȘ On BEST ANSWER

You will have to refresh the sys.path but you can use the site.py to do it.

import site 
reload(site)