-2

The same question has been asked a number of times but I couldn't find the solution.

After I install a package using pip, I am able to import it in python console or python file and it works as expected. The same package when I try to include in django, it gives import error. Do I need to modify settings.py file or any requirement that I need to add? I am driving django with the help of virtual env.

Eg: I am using BeautifulSoup and I am trying to import from bs4 import BeautifulSoup and I am getting error ImportError: No module named 'bs4'

This error only comes in django. I am not able to figure out why this is happening.

Screenshot attached for reference.

1. python console - shows no error

python2 and python3 console

2. django console- import error

django console

I am sorry as it is difficult to read the console but any other thing that I can include which will help me make myself more clear will be appreciated.

Sumit Sahay
  • 504
  • 4
  • 22
  • 4
    Copy-paste the output rather than adding screenshots. Screenshots are evil (difficult to read, accessibility issues, text can't be indexed for search) – Jérôme Oct 11 '16 at 07:51
  • There is no output to show. This is just an import error. – Sumit Sahay Oct 11 '16 at 08:08
  • 1
    It is the error that you should copy and paste. As Jérôme says, screenshots are evil. – Daniel Roseman Oct 11 '16 at 08:34
  • The screen shot is just to verify that both python2 and python3 shows no import error but django does. Maybe from the error in django some might understand the issue which I couldn't – Sumit Sahay Oct 11 '16 at 08:36
  • why downvotes? how better was i supposed to ask this ques? – Sumit Sahay Oct 11 '16 at 09:25
  • There are already two comments suggesting that you copy-paste the output rather than use screenshots. I downvoted your question because you didn't update your answer as requested. – Alasdair Oct 11 '16 at 10:42
  • I beleive yr case is here: [https://stackoverflow.com/questions/11783875/importerror-no-module-named-bs4-beautifulsoup](https://stackoverflow.com/questions/11783875/importerror-no-module-named-bs4-beautifulsoup) – Kos Apr 10 '19 at 06:50

1 Answers1

2

You don't show either the code of your site or the command you ran (and the URL you entered, if any) to trigger this issue. There's almost certainly some difference between the Python environment on the command line and that operating in Django.

Are you using virtual environments? If so, dependencies should be separately added to each environment. Were you operating from a different working directory? Python usually has the current directory somewhere in sys.path, so if you changed directories it's possible you made bs4 unavailable that way.

At the interactive Python prompt, try

import bs4
bs4.__file__

That will tell you where bs4 is being imported from, and might therefore give you a clue as to why it's not available to Django.

holdenweb
  • 33,305
  • 7
  • 57
  • 77
  • The file is already there in site-packages of both python2 and python3. All the packages previously downloaded using `pip` is working except this one. Is there something obvious I am missing? – Sumit Sahay Oct 11 '16 at 08:37
  • Try printing `sys.path` from both environments and seeing if hte difference accounts for your problem. – holdenweb Oct 11 '16 at 09:07