Python - Metmatplotlib error - python

I am using Windows 7. I have installed Python 3.4 and Metmatplotlib.
I have tried the below code
from pylab import *
plot([1,2,3])
show()
But i am getting the follwoign error
Traceback (most recent call last):
File "E:/Work/Python/Training/Samples.py", line 1, in <module>
from pylab import *
File "C:\Python34\lib\site-packages\pylab.py", line 1, in <module>
from matplotlib.pylab import *
File "C:\Python34\lib\site-packages\matplotlib\__init__.py", line 169, in <module>
from urllib2 import urlopen
ImportError: No module named 'urllib2'
I have searched for the package urllib2 but not seems to be find it.
Can any one help me
Thanks,

The urllib2 module has been split across several modules in Python 3 named urllib.request and urllib.error. The 2to3 tool will automatically adapt imports when converting your sources to Python 3.
https://docs.python.org/2/library/urllib2.html

Related

Python requests and beautifulsoup module not importing

I have installed python in my mac. When I type python3in terminal and then import requests and bs4 it imports it and run the program correctly.
But when I run it on python file as python3 file_name.py, it gives the following error:
import requests
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/__init__.py", line 52, in <module>
from .packages.urllib3.contrib import pyopenssl
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/packages/__init__.py", line 27, in <module>
from . import urllib3
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/packages/urllib3/__init__.py", line 8, in <module>
from .connectionpool import (
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/packages/urllib3/connectionpool.py", line 3, in <module>
import logging
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/__init__.py", line 28, in <module>
from string import Template
File "/Users/dark_archer/Desktop/src/string.py", line 1, in <module>
n1,n2=map(int,input().split())
ValueError: not enough values to unpack (expected 2, got 0)
I got the same error with both python 3.5 and python 3.6.
The issue is that you named a module string.py so it's confusing the importer because the logging module is also trying to import something from the standard library module string.py. This causes an issue known as "name shadowing" where your locally defined module is loaded instead of the the standard library module.
When your version of string.py gets imported it triggers the code which is causing your error.
As an easy fix, try to rename your string.py module to something else.
For more info on name shadowing check out the "The name shadowing trap" section of this link: http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html

Boilerpipe import error urllib2

I successfully installed JPype and Boilerpipe Python wrapper.
My JAVA_HOME path is correct (as far as I know).
I created a python file with the following code:
from boilerpipe.extract import Extractor
extractor = Extractor(extractor='ArticleExtractor', url='http://edition.cnn.com/2016/02/09/politics/new-hampshire-primary-highlights/')
extracted_text = extractor.getText()
print(extracted_text)
I am getting this error when I run python3 boiler_test.py
Traceback (most recent call last):
File "boiler_test.py", line 1, in <module>
from boilerpipe.extract import Extractor
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/boilerpipe/extract/__init__.py", line 2, in <module> import urllib2
ImportError: No module named 'urllib2'
How can I solve this?
Thank you.

different behaviour between ipython and python shell for importing paraview

I'm trying to use paraview (http://www.paraview.org/) with python scripting in IPython.
When I'm trying to use it in a python shell it works just find but when I'm trying in IPython I've got the following error:
>>> import paraview.simple
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/gaspard/OpenFOAM/ThirdParty-2.2.2/platforms/linux64Gcc/paraview-3.12.0/lib/paraview-3.12/site-packages/paraview/simple.py", line 39, in <module>
import servermanager
File "/home/gaspard/OpenFOAM/ThirdParty-2.2.2/platforms/linux64Gcc/paraview-3.12.0/lib/paraview-3.12/site-packages/paraview/servermanager.py", line 43, in <module>
import paraview, re, os, os.path, new, sys, vtk
File "/home/gaspard/OpenFOAM/ThirdParty-2.2.2/platforms/linux64Gcc/paraview-3.12.0/lib/paraview-3.12/site-packages/paraview/vtk/__init__.py", line 1, in <module>
from vtkCommonPython import *
ImportError: libvtkCommonPythonD.so.pv3.12: cannot open shared object file: No such file or directory
>>>
The command I used were:
import sys
sys.path.append('/home/gaspard/OpenFOAM/ThirdParty-2.2.2/build/linux64Gcc/paraview-3.12.0/bin/')
sys.path.append('/home/gaspard/OpenFOAM/ThirdParty-2.2.2/platforms/linux64Gcc/paraview-3.12.0/lib/paraview-3.12/site-packages/')
import paraview.simple
Do you know why I've got a different behavior between the classic python shell and the IPython console? And how I can fix this?
You'll also need to set the LD_LIBRARY_PATH environment variable to point to the directory /home/gaspard/OpenFOAM/ThirdParty-2.2.2/platforms/linux64Gcc/paraview-3.12.0/lib/paraview-3.12/.

Python/Flask error: "ImportError: cannot import name _compare_digest"

With Windows, I am following this Flask tutorial when I came across the following error:
C:\Users\Gregory Gundersen\Documents\Research\flask-test>python run.py
Traceback (most recent call last):
File "run.py", line 2, in <module>
from app import app
File "C:\Users\Gregory Gundersen\Documents\Research\flask-test\app\__init__.py
", line 1, in <module>
from flask import Flask
File "C:\Python27\lib\site-packages\flask\__init__.py", line 21, in <module>
from .app import Flask, Request, Response
File "C:\Python27\lib\site-packages\flask\app.py", line 26, in <module>
from . import json
File "C:\Python27\lib\site-packages\flask\json.py", line 25, in <module>
from itsdangerous import json as _json
File "C:\Python27\lib\site-packages\itsdangerous.py", line 14, in <module>
import hmac
File "C:\Python27\lib\hmac.py", line 8, in <module>
from operator import _compare_digest as compare_digest
ImportError: cannot import name _compare_digest
There are SO questions and answers, but they are for OS X/Django. Has anyone see or resolved this issue for PC/Flask before?
You appear to have half the changes made for issue 21306 (backporting hmac.compare_digest to 2.7).
Your hmac module has the lines:
from operator import _compare_digest as compare_digest
at the top, but your sys.version_info shows you are running Python 2.7.6; quoting our quick chat session:
Me: Next simple check:
import sys
print(sys.version_info)
You: sys.version_info(major=2, minor=7, micro=6, releaselevel='final', serial=0)
The hmac version you have is for Python 2.7.7 and up, however!
You'll want to reinstall your Python; download 2.7.8 and reinstall it to make sure you have the correct binary executable for your standard library files.
This can occur when you have updated python without rebuilding your virtualenv. In this case, just regenerate your virtualenv.

Cannot import name _args_from_interpreter_flags

When I try to import multiprocessing in Python 2.7.5 on OS X 10.6.8, I get this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/__init__.py", line 65, in <module>
from multiprocessing.util import SUBDEBUG, SUBWARNING
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/util.py", line 40, in <module>
from subprocess import _args_from_interpreter_flags
ImportError: cannot import name _args_from_interpreter_flags
I also tried to install python2.7.6 with homebrew, but this error still occurs.
It sounds like a circular import issue. Try adding this to the the rest of your imports:
from subprocess import _args_from_interpreter_flags
There is a comment above the function in subprocess.py:
# XXX This function is only used by multiprocessing and the test suite,
# but it's here so that it can be imported when Python is compiled without
# threads.
May be related.

Categories