importing requests seems to only work in shell - python-3.x

I've been testing some stuff with requests in python 3.7 shell and it works fine but as soon as I try and automate it by putting it into a file and running it, I get this error:
Traceback (most recent call last):
File "C:\Users\Ben\AppData\Local\Programs\Python\Python37\lib\site-packages\simple_http_server\__init__.py", line 9, in <module>
import http.cookies as cookies
ModuleNotFoundError: No module named 'http.cookies'; 'http' is not a package
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Ben\Desktop\burp.py", line 6, in <module>
import requests
File "C:\Users\Ben\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\__init__.py", line 43, in <module>
import urllib3
File "C:\Users\Ben\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\__init__.py", line 8, in <module>
from .connectionpool import (
File "C:\Users\Ben\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\connectionpool.py", line 11, in <module>
from .exceptions import (
File "C:\Users\Ben\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\exceptions.py", line 2, in <module>
from .packages.six.moves.http_client import (
File "C:\Users\Ben\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\packages\six.py", line 203, in load_module
mod = mod._resolve()
File "C:\Users\Ben\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\packages\six.py", line 115, in _resolve
return _import_module(self.mod)
File "C:\Users\Ben\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\packages\six.py", line 82, in _import_module
__import__(name)
File "C:\Users\Ben\Desktop\http.py", line 1, in <module>
import simple_http_server
File "C:\Users\Ben\AppData\Local\Programs\Python\Python37\lib\site-packages\simple_http_server\__init__.py", line 11, in <module>
import Cookie as cookies
ModuleNotFoundError: No module named 'Cookie'
Here's my code:
import json
import requests
data = requests.post("https://api.rec.net:443/api/players/v4/bulk", data={"Ids[]": "836571"})
jsonload = json.loads(data.text)
print(jsonload[0]["Bio"])
Here's both shells side by side

What you're trying to do, import requests, is very simple.
Let's back up and create a simple setup for running your script.
Create a brand new conda environment, or pip virtualenv if you prefer.
Use conda install requests to get one library (plus its transitive deps).
What we're going for is an environment that does not have the six compat lib present, that is, import six fails.
We want it to fail, since that library was causing you grief, as seen in your stack trace.
It renames modules you don't want renamed, including http.
Now, from shell or IDE, use the new environment to run your script.
Finally, since test is a standard module that ships with python, you're probably better off renaming your code to something else, perhaps test1.py.

Just figured out the problem. I had a file in the same directory called http which was causing all the problems. Thanks for all the help everyone :)

Related

How does my requests stopped working in python3?

I had this this python3 script running normally using requests.get. However if I switched to another directory other than the one I am working at this error pops up. I tried reinstall requests using pip3 and rename the file but neither worked for me.
Traceback (most recent call last):
File "bs.py", line 2, in <module>
import requests
File "/usr/local/lib/python3.7/site-packages/requests/__init__.py", line 43, in <module>
import urllib3
File "/usr/local/lib/python3.7/site-packages/urllib3/__init__.py", line 8, in <module>
from .connectionpool import (
File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 3, in <module>
import logging
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/__init__.py", line 40, in <module>
import threading
File "/Users/pavel/Documents/Code/threading.py", line 10, in <module>
url = requests.get(host,headers = headers)
AttributeError: module 'requests' has no attribute 'get'
File "/Users/pavel/Documents/Code/threading.py", line 10, in <module>
url = requests.get(host,headers = headers)
AttributeError: module 'requests' has no attribute 'get'
Here,"/Users/pavel/Documents/Code/" is your working directory?
When importing something python searches the working directory first so, import threading from anywhere would try to import your threading.py which will make circular reference to requests. Right now requests module is also importing other modules so method get isn't yet defined. That's why the error.
Try renaming your script somthing else. You have to always be careful of the names like that, if you are going to use a module don't name your python script with the same name. Also be careful of other basic python modules.

unable to run a python file because of Module not found error despite sourcing the pythonpath

I have been trying to run a python file using rosrun command but unfortunately I am having this module not found error because of a package named rospkg. Until a day before yesterday, it was all working fine but yesterday, ubuntu18.04 has got some updates out of which an update for ubuntu base was also present, since that update I am having this error.
I have tried to check whether I have installed the respective python-rospkg and its says I already have the latest version. I have also using
echo $PYTHONPATH
to check the sourced paths and they are sourced correct, I am not sure whats causing this error.
the error is as follows
Traceback (most recent call last
File
"/home/microbot/catkin_ws/src/spider/spider_control/control.py",
line 5, in <module>
import roslib
File "/opt/ros/melodic/lib/python2.7/dist-
packages/roslib/__init__.py", line 50, in <module>
from roslib.launcher import load_manifest
File "/opt/ros/melodic/lib/python2.7/dist-
packages/roslib/launcher.py", line 42, in <module>
import rospkg
ModuleNotFoundError: No module named 'rospkg'
The shebang for my is as follows,
#!/usr/bin/python3
When i try to run the file by changing the shebang to
#!/usr/bin/python2.7
I get the following error
Traceback (most recent call last):
File
"/home/microbot/catkin_ws/src/spider/spider_control/control.py",
line 3, in <module>
import tensorflow as tf
ImportError: No module named tensorflow
I have installed tensorflow version 1.14 using pip installation. Can anyone please help me solving this error. Thanks in advance
edit:
I have manually tried to set the python path to /usr/lib/python2.7/dist-packages as when I tried to install python-rospkg, it says the its already instaled in that location. Doing that I am having error importing tensorflow and the error is
`Traceback (most recent call last):
File
"/home/microbot/catkin_ws/src/spider/spider_control/control.py", line
3, in <module>
import tensorflow as tf
File "/home/microbot/.local/lib/python3.6/site-
packages/tensorflow/__init__.py", line 22, in <module>
import inspect as _inspect
File "/usr/lib/python3.6/inspect.py", line 41, in <module>
import linecache
File "/usr/lib/python3.6/linecache.py", line 11, in <module>
import tokenize
File "/usr/lib/python3.6/tokenize.py", line 33, in <module>
import re
File "/usr/lib/python3.6/re.py", line 142, in <module>
class RegexFlag(enum.IntFlag):
AttributeError: module 'enum' has no attribute 'IntFlag'
Error in sys.excepthook:`

Builtin module not found when executing python script from cmd (python 3.6.5)

Please can you help, I am trying to run a python script from Windows cmd. The error message I get is:
File "C:\Program Files (x86)\WinPython\python-3.6.5.amd64\lib\site-packages\pandas\compat\__init__.py", line 48, in <module>
import __builtin__ as builtins
ModuleNotFoundError: No module named '__builtin__'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\IndexTrader\code\futures_calc.py", line 16, in <module>
import pandas as pd
File "C:\Program Files (x86)\WinPython\python-3.6.5.amd64\lib\site-packages\pandas\__init__.py", line 23, in <module>
from pandas.compat.numpy import *
File "C:\Program Files (x86)\WinPython\python-3.6.5.amd64\lib\site-packages\pandas\compat\__init__.py", line 61, in <module>
import http.client as httplib
File "C:\Program Files (x86)\WinPython\python3.6.5.amd64\lib\http\client.py", line 71, in <module>
import email.parser
I run the python script within Spyder and python and the code executes with no problem or errors.
I am not sure what builtin package it is referring to. I beleieve the issue is with the pandas package (which I uninstalled and reinstalled).
I have successfully run the script from cmd before, so I am not sure what the issue is. I have also reinstalled WinPython, but this has not helped.
I am not sure what to do, I checked the system paths and everything is in order, I also added a PYTHONPATH as a System variable, but no luck.
Any ideas?

requests module not working in pycharm (only)

I was trying to create a web scraping program , which uses bs4 and requests library. What i noticed that whenever pycharm encounters import requests it just gets stuck and throws an error when i press return .The error is :
>>> import requests
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\HP\64bit\lib\site-packages\requests\__init__.py", line 43, in <module>
import urllib3
File "C:\Users\HP\64bit\lib\site-packages\urllib3\__init__.py", line 8, in <module>
from .connectionpool import (
File "C:\Users\HP\64bit\lib\site-packages\urllib3\connectionpool.py", line 3, in <module>
import logging
File "C:\Users\HP\AppData\Local\Programs\Python\Python36\Lib\logging\__init__.py", line 28, in <module>
from string import Template
File "C:\Users\HP\PycharmProjects\Automate\string.py", line 28, in <module>
t = int(input())
ValueError: invalid literal for int() with base 10: ''
>>>
Trying to do the same in IDLE shell or cmd window works just fine
I'm using windows 10 64-bit , python 64-bit 3.6.2rc1 & Pycharm 2017.1.4 community edition.
Your PYTHONPATH is screwed up, which is probably due to PyCharm's option to add the project root to PYTHONPATH (the option is in the "run" settings).
Due to the messed-up PYTHONPATH, the string import inside requests imports your string.py instead of the same module from the stdlib (the traceback clearly shows this by the way).
Easiest solution? Don't have top-level .py files, put them inside a package. That way only your package will be importable as a top-level package in PYTHONPATH but you don't end up having tons of new top-level modules.

Odoo installation Magento Connector

I am new to Odoo/OpenERP. I am trying to install odoo and odoo-magento-connector in ubuntu 14.04 using this tutorial http://www.theopensourcerer.com/2014/09/how-to-install-openerp-odoo-8-on-ubuntu-server-14-04-lts/ I am getting stuck in Step 6, /opt/odoo/openerp-server after typing this command i get error shown in the below
Traceback (most recent call last):
File "/opt/odoo/openerp-server", line 2, in
import openerp
File "/opt/odoo/openerp/init.py", line 76, in
import modules
File "/opt/odoo/openerp/modules/init.py", line 27, in
from . import db, graph, loading, migration, module, registry
File "/opt/odoo/openerp/modules/loading.py", line 44, in
from openerp.modules.module import initialize_sys_path, \
File "/opt/odoo/openerp/modules/module.py", line 37, in
import unittest2
File "/usr/local/lib/python2.7/dist-packages/unittest2-1.1.0-py2.7.egg
/unittest2/init.py", line 40, in
from unittest2.collector import collector
File "/usr/local/lib/python2.7/dist-packages/unittest2-1.1.0-py2.7.egg/
unittest2/collector.py", line 3, in
from unittest2.loader import defaultTestLoader
File "/usr/local/lib/python2.7/dist-packages/unittest2-1.1.0-py2.7.egg/
unittest2/loader.py", line 13, in
from unittest2 import case, suite, util
File "/usr/local/lib/python2.7/dist-packages/unittest2-1.1.0-py2.7.egg
/unittest2/case.py", line 10, in
import traceback2 as traceback
ImportError: No module named traceback2`
Just saw this old when i had the same issue
It is a python module
pip search traceback2
You can install like this (use sudo)
pip install traceback2

Resources