Reputation: 657
i wanted to send a short code i was proud of to a fellow student and realized he won't be able to run it since there is no reason for him to have installed the library. installing is of course super easy - but i realized this could happen often, mainly with beginners - wanted to build a simple function for it:
def smart_import(package_name):
try:
import package_name
except ImportError:
import pip
pip.main(['install', package_name])
problem is that i dont really know how to pass in the name of the package as a value that could be called by import thought of converting a string back but that seems more complicated then i thought
Upvotes: 2
Views: 434
Reputation: 362726
This is a bad idea for many reasons, the main one being that people generally don't expect a Python function call to automatically attempt to install software on their machine.
Here are some other problems this approach has:
dateutil
module is provided by python-dateutil
distribution.smart_import
from another module, it will leave the name imported into the wrong namespace.setuptools
provides setuptools
, easy_install
, and pkg_resources
.pip
itself may not be installed.--user
, and your script itself can't know that.pip
might need to change depending on whether or not you are within a virtualenv.What to do instead:
Package your code up into its own distribution. Write a setup.py
which mentions the dependencies using the install_requires
argument to in the setup
call. Ask your fellow student to pip install mypackage
, and the dependencies will be collected at install time rather than at runtime. You can send your distribution directly to them (as a mypackage-0.1.tar.gz
file or as a wheel). Or you can upload it to an index such as the test pypi.
The Python Packaging User Guide is a beginner-friendly resource describing how to create and upload your own distribution.
Upvotes: 9