No module named 'importlib.util' with Python 3.8 - python

I encountered the following error:
File "D:\python-win\lib\distutils\util.py", line 9, in <module>
import importlib.util
ModuleNotFoundError: No module named 'importlib.util'
I am using python 3.8 (x64) on a windows 10 machine
D:\test> python --version
Python 3.8.10
and the latest pip
(.venv) D:\test> pip --version
pip 21.3.1 from d:\test\.venv\lib\site-packages\pip (python 3.8)
As the above message indicates the error is originating from util.py which is included in the lib/dist folder of the python 3.8 distribution for windows. The offending block is
"""distutils.util
Miscellaneous utility functions -- anything that doesn't fit into
one of the other *util.py modules.
"""
import os
import re
import importlib.util
import string
import sys
from distutils.errors import DistutilsPlatformError
from distutils.dep_util import newer
from distutils.spawn import spawn
from distutils import log
from distutils.errors import DistutilsByteCompileError
All of the info I could find indicated importlib was included with python 3+, but I am seeing this error anyway.
Is there something else I am missing?

Related

Why I get "ImportError" only when I run a python script?

I have a Python script where I import datadog module. When I run python datadog.py, it fails with ImportError: cannot import name statsd. The script starts with following lines:
import os
import mysql.connector
from time import time
from datadog import statsd
Actual error messages are following:
$ python /mnt/datadog.py
Traceback (most recent call last):
File "/mnt/datadog.py", line 5, in <module>
from datadog import statsd
File "/mnt/datadog.py", line 5, in <module>
from datadog import statsd
ImportError: cannot import name statsd
But when I'm in Python shell (started by python command), I can successfully run from datadog import statsd. What's the difference here?
By the way, I have proper Python packages installed in my computer:
$ pip freeze | egrep 'datadog|mysql'
datadog==0.17.0
mysql-connector==2.1.6
$ python --version
Python 2.7.5
The problem is that your script is named datadog.py. So when it imports the module datadog, it imports itself.
First install statsd by
pip install statsd
then do
import statsd

In Python 2.7, "Import Error No module named io" after installing wq.io

In python, I have installed the module wq.io.
How come when I type import wq.io in the Python console, it says the following error:
ImportError: No module named io
When I type import wq or import io, the Python console has no import errors.
Can anyone explain the discrepancies?

Can not install "posix" package using pip

I installed Anaconda on my PC (windows), then I tried to run the following:
import pdb
import sys
import os
import posix
sys.path.append(os.path.join(posix.environ['HOME'], 'mlprojects', 'swig', 'src'))
sys.path.append(os.path.join(posix.environ['HOME'], 'mlprojects', 'datasets', 'python'))
sys.path.append(os.path.join(posix.environ['HOME'], 'mlprojects', 'mlopy', 'netlab'))
##ENDSETUP
import ndlml as nl
import ndlwrap as nw
import numpy as np
import datasets
import netlab
import matplotlib.pyplot as pp
import matplotlib.mlab as ml
import matplotlib.axes
import math
After running the above code, I got the following error:
File "C:/Users/yat/test.py", line 9, in <module>
import posix
ImportError: No module named posix
When I tried pip install posix, I got the following message:
Collecting posix
Could not find a version that satisfies the requirement posix (from versions)
No matching distribution found for posix`
How do I install posix package, and where to find all installed packages in Anaconda?
That's a built-in module that's not available on Windows. Look at the documentation:
Do not import this module directly. Instead, import the module os,
which provides a portable version of this interface. On Unix, the os
module provides a superset of the posix interface. On non-Unix operating
systems the posix module is not available, but a subset is always
available through the os interface. Once os is imported, there is
no performance penalty in using it instead of posix. In addition,
os provides some additional functionality, such as automatically calling
os.putenv when an entry in os.environ is changed.
So just replace posix with os and you should be good to go.
To list all the installed packages with Anaconda, look at the docs:
List all of your packages in the active environment:
conda list
To list all of your packages installed into a non-active environment
named snowflakes:
conda list -n snowflakes

Python script can't open zmq-module

I've a problem with a python-script. In a script, i have:
import zmq
from datetime import datetime
import pytz
import bottle
import logging
from bottle import response
But, when i run this script, i see:
ImportError: No module named zmq
I've also tried:
import pyzmq
When i run: pip install pyzmq, it display's that the package already is installed.
But it doesn't works. The probleem arose after i have updated Python 2.6.x to Python 2.7. I think, the plugins are not included in the upgrade?????
Does anyone have an idea?

MySQLdb installed from pypm doesn't work with 32-bit python on Mac OSX

Before proceeding further, here are some details of my Mac and the installed python (ActivePython) and wxpython versions:
Mac version: 10.6.6
Python version: 2.7.1 (ActiveState Python)
wxPython version: wxPython2.8-osx-unicode-py2.7
Here is a small code that I wrote to test:
#! /usr/bin/env arch -i386 /usr/local/bin/python
import time
import random
import re
import wx
from wx.lib.mixins.listctrl import ListCtrlAutoWidthMixin
import serial
import itertools
import datetime
import sys
import os
import MySQLdb
print "Hello World"
Here is the error that I got from running it:
Traceback (most recent call last):
File "./sar.pyw", line 13, in <module>
import MySQLdb
File "/Users/ven/Library/Python/2.7/lib/python/site-packages/MySQLdb/__init__.py", line 19, in <module>
import _mysql
ImportError: dlopen(/Users/ven/Library/Python/2.7/lib/python/site-packages/_mysql.so, 2): Symbol not found: _mysql_affected_rows
Referenced from: /Users/ven/Library/Python/2.7/lib/python/site-packages/_mysql.so
Expected in: flat namespace
in /Users/ven/Library/Python/2.7/lib/python/site-packages/_mysql.so
As you can see I am using 32-bit python in the shebang line because I need to work with wxpython which runs only on 32-bit. Now, the only package that has problem with the 32-bit python is the MySQLdb which has been installed using:
pypm install mysql-python
If I just removed the line import MySQLdb from the above piece of code, it runs perfectly and prints "Hello World".
If I removed "arch -i386" from the shebang line, the wxpython package doesn't work. I want to use both wxpython and MySQLdb at the same time. How do I do it?
Duplicate ;-)
This is a known bug -- the mysql-python package is built only for 64-bit at the moment.
I want to use both wxpython and MySQLdb at the same time. How do I do it?
wxPython 2.8 doesn't support 64-bit (due to using Carbon), this is why the PyPM package is built for only 32-bit. You could try using wxPython 2.9.
Workaround: remove wxpython using pypm (pypm uninstall wxpython), and then install the development cocoa binary wxPython2.9-osx-cocoa-py2.7 from wxpython.org.

Categories