import requests and Beautifulsoup not working - python

I am unable to import any installed module in my python script "scrapper.py".
I followed these steps:
Inside scrapvenv folder created a virtualenv named scrapvenv
activated scrapvenv and pip install requests and pip install bs4
created a python file "Scrapper.py" inside the base scrapvenv folder
though the pip list shows that these module are installed But import doesn't work.
Vscode shows that the python interpreter is 3.9.4 64 bit

If you are using VSCode, and assuming that you are running your script from the terminal, make sure that VSCode has selected the correct interpreter from your virtual environment, by making sure that the command line contains your environment name in brackets, like so:
(scrapvenv) C:\...\scrapenv\>
If it does not, make sure to select it manually by pressing Ctrl+Shift+P, searching for Python: Select Interpreter, and choose from a list, or browse manually if you don't see your environment in the list

Related

Module not found error in Visual Studio Code despite the fact that I have just installed it

I want to run a python script with the cherrypy module. I use pip install cherrypy to install it and all looks ok. Then I click the "Run python file" button and I face the error ModuleNotFoundError: No module named 'cherrypy'.
Trying to reinstall cherrypy I see many Requirement already satisfied responses.
If it helps I have already edit, in the user path variables panel, the Path variable and added C:\Users\username\AppData\Local\Programs\Python\Python37-32\Scripts as a path.
edit: I suspect some problem with the path. So i run the following.
>>> import os
>>> import sys
>>> os.path.dirname(sys.executable)
'C:\\Python25'
it returns: 'C:\\Users\\username\\AppData\\Local\\Microsoft\\WindowsApps\\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0'
Have you tried to install cherrypy using C:\Users\username\AppData\Local\Programs\Python\Python37-32\Scripts\pip install cherrypy
When I install py libraries I always run where python and find the right python copy it and modify the path so it goes in Scripts then run pip install cherrypy
Also I noticed that when I install py libraries it seems VSCode won't recognize it even though it runs. To fix this you have to restart VSCode again
Another possibility is that your VSCode isn't using correct version of Python. To check this look at VSCode bottom left you should see your python version. If VSCode can't recognize it you will have to enter the Python path manually by Open Command Palette Ctrl+Shift+P and choose Python: Select Interpreter then click enter Interpreter path

Module can be imported in python but cannot be used from command line

This is essentially the opposite of this problem: Import module works in terminal but not in IDLE
If I start a python session and try to import flask, then it works just fine. Despite that, if I just run flask from the terminal, it is not recognized.
I have confirmed that the locations which pip installs to is on my system path. (I'm on Windows 10 and I typed $Env:Path into Powershell to see that, for example, C:\Users\Zack\AppData\Roaming\Python\Python37\site-packages is in the system path.)
I am also sure that I did pip install --user flask not inside a virtual environment. When I run import flask; print(flask.__file__ I see C:\Users\Zack\AppData\Roaming\Python\Python37\site-packages)
Any other ideas for what could be going wrong?
Edit: I'm trying to run flask run. To check the path python uses, I did (in python) import sys; for i in sys.path: print(i)
As Python language based on Python interpreter, before you use Python, it is necessarily to activate the Python env.

Installing Python packages for Visual Studio Code

I'm trying to import a package called "termcolor" that can run on VSC. I know the import is working because when I run the program from my mac terminal it executes fine. However, when I run from VSC, I get the ImportError "no module named termcolor".
I know it has been installed in the my virtual environment where I am building the program:
Kaylas-iMac:Ermes Marana rahme$ source "/Users/rahme/Desktop/Programming/Python/Ermes Marana/env/bin/activate"
(env) Kaylas-iMac:Ermes Marana rahme$ python3 -m pip install termcolor
Requirement already satisfied: termcolor in ./env/lib/python3.7/site-packages (1.1.0)
And I think I'm importing from the correct path. I saw in another post to use sys.path.append() to explicitly look for packages in the correct path, so I tried that. Here is my code:
import sys
sys.path.append('users/Rahme/Desktop/Programming/Python/Ermes\ Marana/env/lib/python3.7/site-packages')
import termcolor
print(termcolor.colored(text = "I'm Rahme", color ="red", on_color="on_grey"))
sys had a warning about the space in the directory /Ermes\ Marana and suggested adding an r to the backslash which I did and nothing changed. What am I doing wrong?
How can I install packages on VSC in the future so that I can use the built in run in terminal?
Just because you switched to your virtual environment in the mac terminal doesn't mean that the VSC terminal is operating in that virtual environment. To get VSC to know you want it to run in your environment, you need to hit ctrl+shift+p to pull up the command pallette. Then you type python: select interpreter. You will get a list of all the virtual environments that VSCode can see and you just select your environment from that list. Once you do that, VSC should operate fine inside your environment.
try
sys.path.append(r'users/Rahme/Desktop/Programming/Python/Ermes/
Marana/env/lib/python3.7/site-packages')
What is the purpose of the space?
Edit: keep the space if it is part of the folderpath
Please do see the r I have added infront of the folderpath

Python cannot find module even when path is appended to sys.path

Having some weird troubles installing python modules on my work computer (read: no admin/root rights), I'm using 2.7.5. I downloaded and unpacked the tarball and ran 'setup.py', but it had no effect: When I open the python shell, it can't find the module (this specific one is fuzzywuzzy). However, if I right click -> edit with IDLE the setup.py, and then run the shell from that file, it loads and works perfectly fine. Or, if I then open a new file from that shell, use the module and run it, it works fine. -__-
I've tried using:
import sys
sys.path.append('path here')
to append the location where the module is installed, but this doesn't help, nor does the path stay in the sys.path list when I close/reopen the shell.
This is actually driving me insane. Can anyone help? I'm relatively new to programming and python.
The best and easy way provided by python to install/uninstall packages is to use PIP.
use this
python -m pip install packagename==version
same way to uninstall
python -m pip uninstall packagename==version
if you are using windows you need to set path variable first usually python file will be in path C:\Python27 to set path variable
PATH=%PATH%;C:\Python27;

ImportError: No module named 'bottle' in PyCharm

I installed bottle on Python 3.4 with pip install. In the terminal, when I do:
$ python3.4
>>>import bottle # shows no import error
>>>
but when I do it in PyCharm, it says:
import bottle ImportError: No module named 'bottle'
in your PyCharm project:
press Ctrl+Alt+s to open the settings
on the left column, select Project Interpreter
on the top right there is a list of python binaries found on your system, pick the right one
eventually click the + button to install additional python modules
validate
In some cases no "No module ..." can appear even on local files. In such cases you just need to mark appropriate directories as "source directories":
The settings are changed for PyCharm 5+.
Go to File > Default Settings
In left sidebar, click Default Project > Project Interpreter
At bottom of window, click + to install or - to uninstall.
If we click +, a new window opens where we can decrease the results by entering the package name/keyword.
Install the package.
Go to File > Invalidate caches/restart and click Invalidate and Restart to apply changes and restart PyCharm.
Settings:
Install package:
I am using Ubuntu 16.04. For me, it was the incorrect interpreter, which was by default using the virtual interpreter from the project.
So, make sure you select the correct one, as the pip install will install the package to the system Python interpreter.
PyCharm 2019.3, my solution is below:
For me, none of the above worked, and curiously even within one file some imports worked, some didn't:
from folder1.folder2.folder3.my_python_file import this_function # worked
from folder1.folder2.folder3.my_python_file import that_function # didn't work
Follow the above advice, but if it doesn't fix it additionally, (in PyCharm) click File >> Repair IDE and confirm all the 6 steps one after another.
I had virtual env site package problem and this solved it:
In the case where you are able to import the module when using the CLI interpreter but not in PyCharm, make sure your project interpreter in PyCharm is set to an actual interpreter (eg. /usr/bin/python2.7) and not venv (~/PycharmProject/venv/...)
I had the same problem, I tried all fixes like installing from the project interpreter and installing from python console, nothing worked. What worked was just going to the project folder from the terminal and installing it from there.

Categories

Resources