0

In my Python script the module BeautifulSoup 4 is required to be able to execute correctly, how am I supposed to deploy it from inside my script? Is that even possible? What would be the easiest and most common way to automatically install the dependency?

Ramchandra Apte
  • 4,033
  • 2
  • 24
  • 44

3 Answers3

0

Sounds like you need to read up a bit on modules (http://docs.python.org/2/tutorial/modules.html), but basically you install the BeautifulSoup module (http://www.crummy.com/software/BeautifulSoup/):

$ easy_install beautifulsoup4

and simply import it in your script:

import bs4
soup = bs4.BeautifulSoup(some_html_doc)

or

from bs4 import BeautifulSoup
soup = BeautifulSoup(some_html_doc)

You can create various environments with different installations of modules and even Python itself with virtualenv, but I'd hold off on that until you are more comfortable.

wwwslinger
  • 936
  • 8
  • 14
0

If you need to distribute your package, you need to understand packaging python http://www.scotttorborg.com/python-packaging/.

From the script itself, what I can recommend is show a message saying beautifulsoup4 is required. Something like:

try:
    import bs4
except ImportError:
    print "This script requires Beautifulsoup4. Please install it."
sagarchalise
  • 992
  • 8
  • 14
0

Dependencies for a library

If you are developing a library that others will install, make sure to read this:

Distributing Python Modules: Writing the Setup Script

Own app / script dependencies

If you are trying to deploy it by yourself, the common practice is to use pip command-line tool and list dependencies in requirements.txt file, like this:

requests==1.2.3
simplejson>=2.3,<2.5

Then, do this during deployment:

$ pip install -r requirements.txt

More on dependencies: separating environment

It is also worth noting, that to isolate your app/module/script from the other Python apps, it is worth using virtualenv.

More on dependencies: distributing with external modules

This also allows you to distribute external modules with your own source. For more see: How to pip install packages according to requirements.txt from a local directory?

Community
  • 1
  • 1
Tadeck
  • 132,510
  • 28
  • 152
  • 198