Requests module for python not being recognized - python

I am on a Mac.
I installed the requests module to do some web scraping but whenever I run the file it is telling me that it doesn't recognize the requests module. I then checked if the module is installed and it was. I've attached a screenshot.
And here is the error message
File "scraper.py", line 1, in <module>
import requests
ImportError: No module named requests

The issue is with the different versions of Python I have. Mac ships with 2.7 by defualt and even though I installed the requests module it was installed for the default python version.
So what I did was
Installed the latest Python version
Changed the default Interpreter Path in Vscode . Here is the official Vscode guide https://code.visualstudio.com/docs/python/environments
And finally installed requests for Python 3 using this command in the terminal
sudo python3 -m pip install requests
You may need to update your pip too
pip install --upgrade pip
I still think I need to manage the Python environments better but that is for another time.

Related

Python2.7 no smb module. Cant locate

Running kali 2020, I am trying to run CVE-2007-2447 which is a python2.7 script, which says:
import smb
from smb.SMBConnection import SMBConnection
I saved the script to a file and ran it, but I get ImportError: No module named smb. In the script, it says install pysmb by running pip install --user pysmb.
I did this, but still the same error message.
I tried to locate pysmb and found these packages:
$ locate pysmb
/usr/lib/python3/dist-packages/pysmbc-1.0.23.egg-info
/usr/lib/python3/dist-packages/pysmbc-1.0.23.egg-info/PKG-INFO
/usr/lib/python3/dist-packages/pysmbc-1.0.23.egg-info/dependency_links.txt
/usr/lib/python3/dist-packages/pysmbc-1.0.23.egg-info/top_level.txt
/usr/share/system-config-printer/pysmb.py
/usr/share/system-config-printer/__pycache__/pysmb.cpython-39.pyc
I have had a problem before where packages only install for python3 and I have to copy the folders to python2.7. I tried that for this without success.
When I googled module smb, it comes up with samba. I installed the samba package but still nothing. Does anyone know how I can get smb module to python2.7?
You need to install using the Python 2.7 pip. Use the following command:
pip2.7 install pysmb
Pip no longer supports Python 2.
pip documentation v21.0.1
You need to use python 2 pip to install the module, instead of installing it for python 3. See How to use pip with Python 3.x alongside Python 2.x
.
In my case, the problem was simple.
I named the python script file 'smb.py'. 'smb.py' and'pysmb' were in conflict.

Installed Selenium on Ubuntu, but can't import it

I am using Ubuntu Server 18.04 LTS(HVM)-free tier to run my python script. I connect with the Key to the server with Putty. I manage my files loaded onto the the server using FileZilla. After I install python on the server with sudo apt install python3 I install Selenium using pip install -U selenium. The process is a success. I then load my python script through FileZilla and then try to run the python script. Like most that have asked I get the error:
Traceback (most recent call last):
File "BinaryAutomation.py", line 1, in <module>
from selenium import webdriver
ModuleNotFoundError: No module named 'selenium'
Now please understand this. I have tried to install selenium manually by installing and unachieving the file found here, which basically downloads the setup to be loaded using python setup.py install. I do not have permission to paste anything under tmp\usr\bin I have tried that. How do i get around this problem?
You probably have other versions of python/pip installed. Install packages using pythonx -m pip install ... with pythonx your python version (python3 I presume) to avoid installing packages for a version of python that you're not using to run your scripts.
For more details check: Why you should use python -m pip - Snarky
Ubuntu generally follows PEP 394, which says that the command python should be Python 2. So, use python3 instead of python. That extends to PIP too, so I would say to use pip3 instead of pip, but that method is becoming outmoded; use python3 -m pip instead.
This is assuming you haven't changed the python and pip commands.

Im having trouble importing a the request module in Python

I wanted to use the request library for python and I went to windows powershell and put in 'pip install requests'and it said install successful but when I open up an ide it says that it cannot find the import when I try to use it. How do I use a python import installed with the pip command?
suppose virtual environment issue
to check:
under powershell: pip show requests
notice the path it installed
under IDE, open the shell and type: pip show requests
it should show nothing if you inside the virtual environment. then simply pip install requests again, and then pip show requests, you would see the requests module under your venv folder

Import Error: No module called magic yet python-magic is installed

I am trying to edit some code that uses python-magic but I get an Import Error: No module called magic. Before I looked around the Internet and found advise on installing python-magic using pip which I did. I installed python-magic using pip install python-magic and also did pip install libarchive-c successfully.
when I try to do the import on the python shell. I am able to successfully as below;
But when I try to run code that uses this import statement I get an import error for missing magic module as below;
If anyone knows what is happening. Please help.
You have installed magic for Python 2.7, but Diffoscope uses Python 3 and explicitly recommends the package python3-magic in the repositories, which can be installed with sudo apt-get install python3-magic. Modules installed for Python 2.7 are not necessarily shared with Python 3, so you may need to install both versions if you need it for 2.7 as well.
On Ubuntu, you can run Python 3 with python3 and access Python 3's pip installation with pip3 to ensure that you are using the correct version.

"ImportError: no module named 'requests'" after installing with pip

I am getting ImportError : no module named 'requests'.
But I have installed the requests package using the command pip install requests.
On running the command pip freeze in the command prompt, the result is
requests==2.7.0
So why is this sort of error happening while running the python file?
Run in command prompt.
pip list
Check what version you have installed on your system if you have an old version.
Try to uninstall the package...
pip uninstall requests
Try after to install it:
pip install requests
You can also test if pip does not do the job.
easy_install requests
I had this error before when I was executing a python3 script, after this:
sudo pip3 install requests
the problem solved, If you are using python3, give a shot.
One possible reason is that you have multiple python executables in your environment, for example 2.6.x, 2.7.x or virtaulenv. You might install the package into one of them and run your script with another.
Type python in the prompt, and press the tab key to see what versions of Python in your environment.
In Windows it worked for me only after trying the following:
1. Open cmd inside the folder where "requests" is unpacked. (CTRL+SHIFT+right mouse click, choose the appropriate popup menu item)
2. (Here is the path to your pip3.exe)\pip3.exe install requests
Done
if it works when you do :
python
>>> import requests
then it might be a mismatch between a previous version of python on your computer and the one you are trying to use
in that case : check the location of your working python:
which python
And get sure it is matching the first line in your python code
#!<path_from_which_python_command>
Opening CMD in the location of the already installed request folder and running "pip install requests" worked for me. I am using two different versions of Python.
I think this works because requests is now installed outside my virtual environment. Haven't checked but just thought I'd write this in, in case anyone else is going crazy searching on Google.

Categories