1

I have a package that I would like to automatically install and use from within my own Python script.

Right now I have this:

>>> # ... code for downloading and un-targzing

>>> from subprocess import call
>>> call(['python', 'setup.py', 'install'])
>>> from <package> import <name>
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named <package>

Then I can continue like this:

>>> exit()
$ python
>>> from <package> import <name>

And it works just fine. For some reason, Python is able to pick up the package just fine if I restart after running the setup.py file, but not if I don't. How can I make it work without having the restart step in the middle?

(Also, is there a superior alternative to using subprocess.call() to run setup.py within a python script? Seems silly to spawn a whole new Python interpreter from within one, but I don't know how else to pass that install argument.)

2
  • are you using environments at all? Commented Dec 14, 2015 at 20:26
  • @riotburn - Not sure what you're talking about, so I'm guessing no? I normally install everything using pip, just for some reason one SDK I need isn't available via PyPI - instead the company makes you download a .tar.gz from their website and tells you to run setup.py inside. Commented Dec 14, 2015 at 20:28

2 Answers 2

2

Depending on your Python version, you want to look into imp or importlib.

e.g. for Python 3, you can do:

from importlib.machinery import SourceFileLoader
directory_name = # os.path to module
# where __init__.py is the module entry point
s = SourceFileloader(directory_name, __init__.py).load_module() 

or, if you're feeling brave that your Python path knows about the directory:

map(__import__, 'new_package_name')
Sign up to request clarification or add additional context in comments.

5 Comments

+1 on the 'feeling brave' bit - if theres one file wrong in there then the entire import will fall over. I generally go for a loop over the files and import (or import the specific files). I think importlib is only from Python 2.7 and above: stackoverflow.com/questions/34275884/… seems to have a lot more details.
importlib works on python3. I use it in one of my projects.
oh yes, i definitely back the use of import lib - its the map bit that I think care is needed around but it all depends how much you trust your imported lib quality. (Sorry- i mispoke, i meant its only from 2.7 upwards... not just only specific 2.7)
I just noticed one difference thing that changes after I restart python - /usr/lib/python2.7/site-packages/nitro_python-1.0-py2.7.egg gets added to sys.path. Is there some way I can force sys.path to reload without restarting Python? Or would manually inserting that path be my best bet (how can I even find that path in a cross-platform way?)
Oh, Python 2 -- to refresh sys.path, you can import site; reload(site)
-1

Hope this helps,

I downloaded from seaborn from GitHub.

Through command prompt, cd to downloads\seaborn folder

python install setup.py

Then using spyder from anaconda, checked if it was installed by running the following in a console

import pip
sorted(["%s==%s" % (i.key, i.version)
     for i in pip.get_installed_distributions()])

Seeing that it was not there, go to tools and select "Update module names list"

Again trying the previous code in a python console, the lib was still not showing.

Restarting Spyder and trying import seaborn worked.

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.