bob312312
bob312312

Reputation: 1

Installing modules seems to work when running python in terminal but not when running a script

Hi so I downloaded the source code for beautifulsoup4. I moved it to a folder .../Desktop/Python_modules/ which is where I like to keep all the folders for the modules I down load and tried to install it as follows:

  1. went to the directory

  2. ran:

    python setup.py install
    

How comes now when I open python in terminal i can import beautifulsoup4 using "from bs4 import BeautifulSoup" but when i have it in a script which is executed using ./script it gives the following error: 'ImportError: No module named requests'?

And How would I go about installing beautifulsoup4 so that i can run the script using "./"?

Just for completion: I'm using a mac

Upvotes: 0

Views: 384

Answers (1)

larsks
larsks

Reputation: 312253

The behavior you are seeing suggests strongly that there are two different versions of Python installed on your system. If your scripts start with:

#!/usr/bin/python

Then running ./script will always run /usr/bin/python. If you have another Python installed (say, via homebrew, which will give you /usr/local/bin/python) then running python in the terminal will probably get you that version.

So...running python setup.py install will install the module where it is visible to /usr/local/bin/python but not to /usr/bin/python, which is why it works for you when you run python in the terminal but not when you run a script.

You can fix this by running your scripts like this:

python script

Or by modifying the scripts to start with:

#!/usr/bin/env python

Which will look for a python binary in your $PATH rather than using a fixed path.

(Or you can install modules by running /usr/bin/python setup.py install)

Upvotes: 1

Related Questions