9

I downloaded beautifulsoup.py for use on a little project I'm making. Do I need to import this .py file in my project?

Do I just copy and paste the code somewhere inside my current python script?

Thank you for the help.

I found this but it doesn't say anything regarding Windows. http://mail.python.org/pipermail/tutor/2002-April/013953.html

I'm getting this error when using it. I copied and pasted the .py file to the folder where my project was on Windows Explorer, not this happens. Any suggestions? alt text

  • 7
    I'm surprised noone posted this line http://docs.python.org/py3k/tutorial/modules.html along with a "RTFM" comment... –  Oct 20 '10 at 16:40
  • 1
    @Sergio -- the reason for this is that the file is called `BeautifulSoup.2.1.1` -- change it to `BeautifulSoup` and everything should *just work* (TM) – Sean Vieira Oct 20 '10 at 17:38

5 Answers5

11

If it's in the same directory as your little project, all you should need to do is:

import BeautifulSoup

If you are keeping it in some other directory, the easiest way to do it is:

from sys import path
path.append(path_to_Beautiful_Soup)

import BeautifulSoup

Python keeps track of where it is currently, and first looks in the current directory. Then it checks through all of the paths in sys.path for the module in question. If it cannot find it in any of those places, it throws an error.

Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
5

When you install beautifulsoup the canonical way (with easy_install for example, or with a windows installer, if any) the beautifulsoup module will probably be added to your PYTHONDIR\lib\site-packages directory.

This means

import beautifulsoup

should do the trick.

Otherwise, adding beautifulsoup.py (if it's a single file) to your current project directory and then issuing import beautifulsoup should also do the trick.

ChristopheD
  • 112,638
  • 29
  • 165
  • 179
2

You have several choices:

  • you can cut and paste in the code, assuming the license permits etc. however, what happens when the code is updated?

  • you can put the code into the same directory (ie folder) as your code. Then all you need to do is say import beautifulsoup before you try to use it.

  • you can put the code somewhere in the python load path.

Charlie Martin
  • 110,348
  • 25
  • 193
  • 263
0

I've done it before, putting BeautifulSoup.py inside the same directory as the script and import it would work. If you have multiple scripts spread over different directories, put the beautifulsoup file in the root directory and do a relative import.

Pwnna
  • 9,178
  • 21
  • 65
  • 91
0

you have to install a new package correctly in python by using in the command line:

pip install BeautifulSoup 

if you don't know the name of the package use :

pip search beautiful 

and the pip will get all package that have "beautiful" in their name or description ...

One more thing that is very important ; and because you use eclipse (netbeans it's the same) and pydev as i can see; you should refresh the list of package used by pydev when installing a new package by going in the menu to (for eclipse) Window -> Preference -> Pydev -> Interpreter - Python and clicking on Apply why is that ?, so that you can use the full power of pydev correctly (code completion , F3 ...) and because Pydev don't know if a package has been added until you tell him so.

the steps are for eclipse you can do their analog in netbeans right ?

mouad
  • 67,571
  • 18
  • 114
  • 106