Search code examples
pythonbeautifulsoupmodulepippython-import

Can not use bs4 even after installing bs4 in python


I install beautifulsoup and tried to use bs4 in VScode. I check the IDLE where I can find the beautifulsoup but when I try to import in my vscode it is not running here is the picture enter image description here

I am learning python and I am trying to solve this problem for the last 2 days. Please help me to solve this problem. I check this problem elsewhere and the solution over there did not work


Solution

  • A general good thing to do is creating a virtual environment for managing python packages.

    You can then use the virtual environment in VSCode as the python extension can auto-detect them/let you choose one:

    Hope this can help you solve your dependency problem.

    EDIT: as pointed out in a comment the content is links only here, I add the basics as to what you can do to manage venvs.

    On unix-like systems in python3 (you may need to install an OS package to get the venv package, e.g. on Ubuntu sudo apt install python3-venv) you can use the following command to create a venv in the dir .venv:

    python3 -m venv .venv
    

    Then you need to use the new venv, on Unix like OSes to activate the previously created .venv you use:

    source .venv/bin/activate
    

    On windows you run the script:

    ./.venv/Scripts/activate
    

    Then you can install your dependencies in the venv, in the case of bs4:

    pip install beautifulsoup4
    

    And then you can use the venv in VSCode. If you have the python extension VSCode will auto detect venvs in your current workdir. If it does not use ctrl + shift + P to open the command palette and then search for "Python: select interpreter", then give the path to the python binary in your venv e.g. ".venv/bin/python" or for Windows ".venv/Scripts/python.exe"