Anaconda环境中已装BeautifulSoup4/requests却无法导入的问题求助
Hey there, let's work through why your scrape.py script can't find the bs4 and requests modules even though they're clearly installed in your dash conda environment. From your conda list output, both packages are present, so the issue almost certainly relates to which Python interpreter is running your script.
Here are the practical steps to fix this:
1. Run your script with the conda environment's Python explicitly
When you type just scrape.py in the prompt, Windows might default to your system's Python instead of the one tied to your dash environment. Instead, run it like this:
python scrape.py
This ensures you're using the Python interpreter that's part of your activated dash environment.
To double-check which Python is being used, add these lines at the top of scrape.py and run it:
import sys print("Python executable path:", sys.executable)
The output should match the Python path in your dash environment: C:\Users\u815279\AppData\Local\Continuum\anaconda3\envs\dash\python.exe. If it doesn't, that's the root cause.
2. Re-confirm your conda environment is properly activated
Even though your prompt shows (dash), environment activation can sometimes have glitches. Try re-activating it to be sure:
conda deactivate conda activate dash
Then run your script again with python scrape.py.
3. Use the environment's pip for package installs
If you tried re-installing with pip install, you might have used your system's pip instead of the one in the dash environment. To avoid this, always install packages using the environment's Python directly:
python -m pip install beautifulsoup4 requests
This guarantees the packages are installed into the correct environment's site-packages folder.
4. Check for unexpected sys.path changes in your script
If your scrape.py has lines that modify sys.path (like sys.path.append(...)), these could override the default paths pointing to your conda environment's packages. Comment out any such lines and test again.
Your Environment Details (for reference)
(dash) C:\Programs without certification\Ragesh\View\WebScrape>conda list -n dash # packages in environment at C:\Users\u815279\AppData\Local\Continuum\anaconda3\envs\dash: # # Name Version Build Channel asn1crypto 1.3.0 py37_0 anaconda beautifulsoup4 4.8.2 py37_0 anaconda blas 1.0 mkl ca-certificates 2020.1.1 0 anaconda certifi 2019.11.28 py37_0 anaconda cffi 1.14.0 py37h7a1dbc1_0 anaconda chardet 3.0.4 py37_1003 anaconda click 7.0 py_0 conda-forge cryptography 2.8 py37h7a1dbc1_0 anaconda dash 1.7.0 py_0 conda-forge dash-core-components 1.6.0 py_0 conda-forge dash-html-components 1.0.2 py_0 conda-forge dash-renderer 1.2.2 py_0 conda-forge dash-table 4.5.1 py_0 conda-forge flask 1.1.1 py_1 conda-forge flask-compress 1.4.0 py_0 conda-forge future 0.16.0 py_1 conda-forge icc_rt 2019.0.0 h0cc432a_1 idna 2.8 py37_0 anaconda intel-openmp 2019.4 245 itsdangerous 1.1.0 py_0 conda-forge jinja2 2.10.3 py_0 conda-forge markupsafe 1.1.1 py37he774522_0 mkl 2019.4 245 mkl-service 2.3.0 py37hb782905_0 mkl_fft 1.0.15 py37h14836fe_0 mkl_random 1.1.0 py37h675688f_0 numpy 1.17.3 py37h4ceb530_0 numpy-base 1.17.3 py37hc3f5095_0 openssl 1.1.1 he774522_0 anaconda pandas 0.25.3 py37ha925a31_0 pip 19.3.1 py37_0 plotly 4.4.1 py_0 pycparser 2.19 py37_0 anaconda pyopenssl 19.1.0 py37_0 anaconda pysocks 1.7.1 py37_0 anaconda python 3.7.6 h60c2a47_2 python-dateutil 2.8.1 py_0 pytz 2019.3 py_0 pyyaml 5.2 py37he774522_0 requests 2.22.0 py37_1 retrying 1.3.3 py37_2 setuptools 44.0.0 py37_0 six 1.13.0 py37_0 soupsieve 1.9.5 py37_0 sqlite 3.30.1 he774522_0 urllib3 1.25.8 py37_0 anaconda vc 14.1 h0510ff6_4 vs2015_runtime 14.16.27012 hf0eaf9b_1 werkzeug 0.16.0 py_0 conda-forge wheel 0.33.6 py37_0 win_inet_pton 1.1.0 py37_0 anaconda wincertstore 0.2 py37_0 xlrd 1.2.0 py37_0 yaml 0.1.7 vc14_0 conda-forge
Error Output (for reference)
(dash) C:\Programs without certification\Ragesh\View\WebScrape>scrape.py Traceback (most recent call last): File "C:\Programs without certification\Ragesh\View\WebScrape\scrape.py", line 1, in <module> import requests ModuleNotFoundError: No module named 'requests' (dash) C:\Programs without certification\Ragesh\View\WebScrape>scrape.py Traceback (most recent call last): File "C:\Programs without certification\Ragesh\View\WebScrape\scrape.py", line 1, in <module> from bs4 import BeautifulSoup ModuleNotFoundError: No module named 'bs4'
内容的提问来源于stack exchange,提问作者Ragesh Kr

