How to use an external .py file?

I downloaded beautifulsoup.py for use in a small project that I am doing. Do I need to import this .py file into my project?

Am I just copying and pasting code somewhere inside my current python script?

Thanks for the help.

I found this, but it says nothing about Windows. http://mail.python.org/pipermail/tutor/2002-April/013953.html

I get this error when using it. I copied and pasted the .py file to the folder where my project was in Windows Explorer, but it is not. Any suggestions? alt text

+4
source share
5 answers

If it is in the same directory as your small project, all you have to do is:

import BeautifulSoup 

If you store it in another directory, the easiest way to do this is:

 from sys import path path.append(path_to_Beautiful_Soup) import BeautifulSoup 

Python keeps track of where it is now, and first searches the current directory. Then it checks all the paths in sys.path for the module in question. If he cannot find him in any of these places, he gives an error.

+8
source

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

It means

 import beautifulsoup 

gotta do the trick.

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

+4
source

You have several options:

  • you can cut and paste the code, assuming the license allows, etc. However, what happens when the code is updated?

  • you can put the code in the same directory (i.e. the folder) as your code. Then all you have to do is say import beautifulsoup before trying to use it.

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

+1
source

I did this before by adding BeautifulSoup.py to the same directory as the script, and the import will work. If you have several scripts spreading across different directories, put the beautifulsoup file in the root directory and do a relative import.

0
source

you need to install the new package correctly in python using the command line:

 pip install BeautifulSoup 

if you don’t know the package use name:

 pip search beautiful 

and the pip will receive the whole package that has "beautiful" in their name or description ...

Another thing that is very important; and because you are using eclipse (netbeans is the same) and pydev, as I can see; you must update the list of packages used by pydev when installing a new package by going to the menu (for eclipse) Window -> Preferences -> Pydev -> Interpreter - Python and clicking Apply , why is this so ?, so that you can correctly use the full power of pydev (code completion , F3 ...) and because Pydev doesn't know if the package has been added until you tell it.

steps for eclipse, can you make your analog in netbeans right?

0
source

All Articles