Python, nltk: Cannot import mkdtemp - python

I got this error while trying to compile test.py that contains these two commands -
import nltk
from nltk.sentiment.vader import SentimentIntensityAnalyzer
This is the error I got
Traceback (most recent call last):
File "test.py", line 3, in <module>
import nltk
File "C:\Anaconda2\lib\site-packages\nltk\__init__.py", line 89, in <module>
from nltk.internals import config_java
File "C:\Anaconda2\lib\site-packages\nltk\internals.py", line 29, in <module>
from nltk import compat
File "C:\Anaconda2\lib\site-packages\nltk\compat.py", line 206, in <module>
from tempfile import mkdtemp
ImportError: cannot import name mkdtemp
The strange thing is, that these same two commands when executed on the command line work perfectly fine.
What could be the reason for this to happen? And more importantly, how do I fix this?
EDIT:
This is what i got when i executed print(sys.path)
['', 'C:\\Anaconda2\\python27.zip', 'C:\\Anaconda2\\DLLs', 'C:\\Anaconda2\\lib', 'C:\\Anaconda2\\lib\\plat-win', 'C:\\Anaconda2\\lib\\lib-tk', 'C:\\Anaconda2', 'C:\\Anaconda2\\lib\\site-packages', 'C:\\Anaconda2\\lib\\site-packages\\Sphinx-1.3.5-py2.7.egg', 'C:\\Anaconda2\\lib\\site-packages\\win32', 'C:\\Anaconda2\\lib\\site-packages\\win32\\lib', 'C:\\Anaconda2\\lib\\site-packages\\Pythonwin', 'C:\\Anaconda2\\lib\\site-packages\\setuptools-20.3-py2.7.egg']
EDIT2:
I realised that I had a file called tempfile.py in my working directory. Upon deletion everything works fine

Make sure you don't have tempfile.py somewhere in python path; which prevents import of standard library tempfile module.
Also make sure deleting tempfile.pyc if there it is.

Related

ImportError: cannot import name 'winrandom'

I know that this question has already a ton of answers but unfortunately this hasn't fixed the issue for me. I want to run some code using PyCrytpo but winrandom doesn't want to get import.
All the other posts are for earlier releases of python. I am using Python 3.6.5 and renaming code within nt.py file to "from . import winrandom" doesn't do the trick for me.
Do you have any suggestions or is a rollback the only opportunity?
Windows 10
Python 3.6.5
Anaconda
from Crypto.PublicKey import RSA
Traceback (most recent call last):
File "<ipython-input-39-b6fe4d249eef>", line 1, in <module>
from Crypto.PublicKey import RSA
File "C:\Users\Markus\Anaconda3\lib\site-packages\Crypto\PublicKey\RSA.py", line 78, in <module>
from Crypto import Random
File "C:\Users\Markus\Anaconda3\lib\site-packages\Crypto\Random\__init__.py", line 28, in <module>
from Crypto.Random import OSRNG
File "C:\Users\Markus\Anaconda3\lib\site-packages\Crypto\Random\OSRNG\__init__.py", line 34, in <module>
from Crypto.Random.OSRNG.nt import new
File "C:\Users\Markus\Anaconda3\lib\site-packages\Crypto\Random\OSRNG\nt.py", line 28, in <module>
from . import winrandom
ImportError: cannot import name 'winrandom'

'import discord' not working in Python 3.5

I try to import discord in python 3.5, but I get the following traceback:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python\Python35-32\lib\site-packages\discord\__init__.py", line 20, in <module>
from .client import Client, AppInfo, ChannelPermissions
File "C:\Python\Python35-32\lib\site- packages\discord\client.py", line 42, in <module>
from .voice_client import VoiceClient
File "C:\Python\Python35-32\lib\site- packages\discord\voice_client.py", line 65, in <module>
from .gateway import *
File "C:\Python\Python35-32\lib\site- packages\discord\gateway.py", line 31, in <module>
import aiohttp
File "C:\Python\Python35-32\lib\site- packages\aiohttp\__init__.py", line 10, in <module>
from .protocol import * # noqa
File "C:\Python\Python35-32\lib\site- packages\aiohttp\protocol.py", line 11, in <module>
from wsgiref.handlers import format_date_time
ImportError: cannot import name 'format_date_time'
I installed discord inside Python35-32\Scripts using pip install discord. Additionally, I installed asyncio using pip install asyncio in the same directory.
The error pops up in my command prompt, when I type in "python", wait for it to open, and then type "import discord". I'm saying that just to be clear that it's not a "I have a file named XXX.py which clashes with something from the discord library" type of error.
Based on the comments you have added and the line
File "C:\Python\Python35-32\lib\site- packages\aiohttp\protocol.py", line 11, in <module>
from wsgiref.handlers import format_date_time
ImportError: cannot import name 'format_date_time'
The problem seems to be that 'format_date_time' could not be imported from the built in library wsgiref. Turns out that the file ...\Python35-32\lib\wsgiref\handlers.py was completely empty indicating that something corrupted your python installation.
Two possible solutions:
Reinstall your python completely which also has the benefit of solving other potential corruptions that you might not have noticed yet (Note that the underlying cause for this corruption remains unknown)
Go into the Cython git and copy the contents of the handlers.py into the local file. This is a very specific solution however and you should be aware that other parts of your python installation might also be corrupt.
Maybe you wanted to pip install discord.py instead of discord?

Run Python program as administrator from command line

I'm trying to run my program as an administrator from the command line. Seems quite easy but I'm struggling.
I started with this, which works fine launched from the Python IDLE:
import dropbox, pymysql, shlex, shutil, subprocess
import logging
import sshtunnel
from sshtunnel import SSHTunnelForwarder
# Main program starts here
print("Hello World!")
but that gave an error as follows:
pi#jsmd:~ $ sudo python /home/pi/Documents/iot_pm2/iot.py
Traceback (most recent call last):
File "/home/pi/Documents/iot_pm2/iot.py", line 3, in <module>
import dropbox, pymysql, shlex, shutil, subprocess
ImportError: No module named pymysql
After some Googling I added two lines at the start:
import sys
sys.path.append("/usr/local/lib/python3.4/dist-packages/")
This also works fine from the Python IDLE but gives a long error trace when run from the command line... and I'm not good enough at understanding how to read the trace and hence what I can do to fix it. (Plus I don't understand why it works from the IDLE as a non-admin but not from the command line as an admin.) Can anyone help?
pi#jsmd:~ $ sudo python /home/pi/Documents/iot_pm2/iot.py
Traceback (most recent call last):
File "/home/pi/Documents/iot_pm2/iot.py", line 5, in <module>
import sshtunnel
File "/usr/local/lib/python3.4/dist-packages/sshtunnel.py", line 25, in <module>
import paramiko
File "/usr/local/lib/python3.4/dist-packages/paramiko/__init__.py", line 30, in <module>
from paramiko.transport import SecurityOptions, Transport
File "/usr/local/lib/python3.4/dist-packages/paramiko/transport.py", line 50, in <module>
from paramiko.dsskey import DSSKey
File "/usr/local/lib/python3.4/dist-packages/paramiko/dsskey.py", line 26, in <module>
from Crypto.PublicKey import DSA
File "/usr/local/lib/python3.4/dist-packages/Crypto/PublicKey/DSA.py", line 89, in <module>
from Crypto import Random
File "/usr/local/lib/python3.4/dist-packages/Crypto/Random/__init__.py", line 29, in <module>
from Crypto.Random import _UserFriendlyRNG
File "/usr/local/lib/python3.4/dist-packages/Crypto/Random/_UserFriendlyRNG.py", line 38, in <module>
from Crypto.Random.Fortuna import FortunaAccumulator
File "/usr/local/lib/python3.4/dist-packages/Crypto/Random/Fortuna/FortunaAccumulator.py", line 39, in <module>
from . import FortunaGenerator
File "/usr/local/lib/python3.4/dist-packages/Crypto/Random/Fortuna/FortunaGenerator.py", line 35, in <module>
from Crypto.Util import Counter
File "/usr/local/lib/python3.4/dist-packages/Crypto/Util/Counter.py", line 59, in <module>
from Crypto.Util import _counter
ImportError: cannot import name _counter
Thanks.
is the default of python is 3 ? If not try to execute like this: sudo
python3 ... – #Koffee
Thanks to #Koffee we have an answer and this thread can be marked as solved and closed.

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