from ConfigParser import SafeConfigParser ImportError: No module named 'ConfigParser' - python-3.x

I am getting below error:
"from ConfigParser import SafeConfigParser
ImportError: No module named 'ConfigParser''
I am trying to install Mysql-python in python3 but not getting success.

The package is renamed to configparser for PEP 8 compliance and the package you are trying to install doesn't support Python 3.
You can use mysqlclient instead. You can install it using the below command:
pip install mysqlclient
Or one more alternative is there which I won't personally recommend, but just to share it:
# Install configparser
pip install configparser
# Rename it to ConfigParser
sudo cp /usr/lib/python3.6/configparser.py /usr/lib/python3.6/ConfigParser.py
After doing the above, you should be able to download MySQL-python without problems.
P.S: Answer is inspired by the answers to this question

In Python 2 this works:
from ConfigParser import SafeConfigParser
Python is case sensitive and in Python 3 the module was renamed to configparser So you would need import like this:
from configparser import SafeConfigParser
Looks like the library you trying to install is for Python 2. You need to get the Python 3 version.

Related

Import "ruamel.yaml" could not be resolved

I am just a beginner in python.
I have a requirement of converting a JSON Object into a YAML file and trying to import ruamel.yaml.
I did install the command pip3 install ruamel.yaml and my python list package shows that it's available.
site-packages % pip3 list
Package Version
------------------ ---------
certifi 2021.10.8
charset-normalizer 2.0.11
idna 3.3
pip 22.0.3
pytz 2021.3
PyYAML 6.0
requests 2.27.1
ruamel.yaml 0.17.21
ruamel.yaml.clib 0.2.6
setuptools 60.9.3
urllib3 1.26.8
Whereas when I am trying to import and use it in the python script it is giving me below error.
Import "ruamel.yaml" could not be resolved
Could someone please suggest how to resolve this.?
When Python cannot find a module you get an error looking like:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named ruamel.yaml
This might be because you are using some non-standard installation/execution environment, or because you try to run this from an editor. Some of those don't understand the concept of namespaces in Python.
It could also be that pip3 is not installing for the python that you are using to run the program. That is easy to check by installing some other package using pip3 and trying to import that.
In either case make sure you test your program running with the python that corresponds to pip3 ( using type pip3 or which pip3 resp type python ), or install using:
python -m pip install ruamel.yaml
and then run the program with python your_prog.py

How can I install modules in Mac OS Terminal?

I want to install some modules for Telegram Bots and I always get errors like:
File "test.py", line 7, in <module>
import spotipy
ModuleNotFoundError: No module named 'spotipy'
I tried to install spotipy with pip, pip3 and apt.
How is it done? Or generally how do I import modules?
Thank you.
From the spotipy README:
If you already have Python on your system you can install the library simply by downloading the distribution, unpack it and install in the usual fashion:
python setup.py install
You can also install it using a popular package manager with
pip install spotipy
or
easy_install spotipy
Make sure your pip version matches your python version (pip --version and python --version should both be Python3 or Python2).
To import the module, use one of the following statements (see python3 documentation):
import numpy
import numpy as np
from numpy import array

No module named 'email.FeedParser'

I am trying to run a code on AWS Lambda but it is returning me the error: "Unable to import module 'main': No module named 'email.FeedParser'".
My code does not use email feedparser module or function. It just connect to one Google API and download a CSV report.
I've checked my code scope and the reference for this module is being done by httplib2 library and on the email/parser.py from the python standard library.
All required libraries are fully updated in requirements.txt file. The code is also configured by a samTemplate.yaml file to execute in a python 3.7 environment at aws.
Do you guys had experienced this problem before? How can I solve it?
Thank you!
import httplib2
from googleapiclient import discovery
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import run_flow
from urllib.parse import urlencode
import requests
import json
import time as t
import pandas as pd
from datetime import datetime, timedelta
from calendar import monthrange
from dateutil.relativedelta import relativedelta
I had to make sure pip3 was actually installing to python3 and not python2.
Instead of:
pip3 install <package>
I had to do:
python3.8 -m pip install <package>
See answer here: Why pip3 install in python2 sitepackages
I had exactly the same issue...
I did this:
vim ~/.pydistutils.cfg
[install]
prefix=
see here: 24257803 for more info on this step
rm -rf [dependencies_dir]
pip3 install -r requirements.txt --target [dependencies_dir]
Pip3 will install this dependencies to Python3, where pip was installing to Python2 (where some of these packages don't exist). So when deploying to Lambda via serverless the packages weren't included.

Installing pypiwin32 successful; cannot import module

Title says it all;
Tried typical cmd commands like:
pip install pypiwin32
python -m pip install pypiwin32
Python script:
import pip
def install(package):
pip.main(['install', package])
if __name__ == '__main__':
install("pypiwin32") # also tried 'pywin32'
I run the above script and get the message:
Requirement already satisfied: pypiwin32 in 'path'
Then run test command:
import pypiwin32
and get:
builtins.ModuleNotFoundError: No module named 'pypiwin32'
Is there something wrong with my path? Do i HAVE to install it as a .whl file?
Not sure what I am doing wrong here. I am wanting a module to convert text to speech.
Ok, not sure if i should answer my own question or delete this, but it may help someone in the future.
I went into my python path and checked my .pyd files, it turns out there was no module called pypiwin32. instead the module is called win32com.pyd
So using the following code:
import win32com.client as wincl
wincl.Dispatch("SAPI.SpVoice").Speak("Speak English, parle français")

ImportError: No module named 'psycopg2._psycopg'

When I try to import psycopg2 it show below log for me:
Traceback (most recent call last):
File "D:/Desktop/learn/python/webcatch/appserver/testpgsql.py", line 2, in <module>
import psycopg2
File "D:/Desktop/learn/python/webcatch/appserver/webcatch/lib/site-packages/psycopg2-2.6.1-py3.5-win32.egg/psycopg2/__init__.py", line 50, in <module>
from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID
ImportError: No module named 'psycopg2._psycopg'
How can I solve it?
My platform is win10 (64) and version is python 3.5
Eureka! I pulled my hair out for 2 days trying to get this to work. Enlightenment came from this SO Question. Simply stated, you probably installed psycopg2 x64 version like I did, not realizing your python version was 32-bit. Unistall your current psycopg2, then:
Download: psycopg2-2.6.1.win32-py3.4-pg9.4.4-release.exe from HERE, then run the following in a Terminal:
C:\path\to\project> easy_install /path/to/psycopg2-2.6.1.win32-py3.4-pg9.4.4-release.exe
C:\path\to\project> python manage.py makemigrations
C:\path\to\project> python manage.py migrate
You may also need to (re)create super user with:
C:\path\to\project> python manage.py createsuperuser
I had the same problem, solved it in this way:
Reinstall the package psycopg2 using pip (by default installed with python 3)
On Linux:
pip uninstall psycopg2
Confirm with (y) and then:
pip install psycopg2
On Windows I add the prefix ('python -m') to the commands above.
I think the problem occurs when you change the version of Python. (Even between minor versions such as Python 3.5 and 3.6).
I am using psycopg in an AWS Glue Job, where is harder to follow the instructions listed in the other answers.
What I did is installing psycopg2-binary into a directory and zip up the contents of that directory:
mkdir psycopg2-binary
cd psycopg2-binary
pip install psycopg2-binary -t .
# in case using python3:
# python3 -m pip install --system psycopg2-binary -t .
zip -r9 psycopg2.zip *
I then copied psycopg2.zip to an S3 bucket and add it as an extra Python library under "Python library path" in the Glue Spark job.
I then launched the job with the following script to verify if psycopg2 is present (the zip file will be downloaded by Glue into the directory in which the Job script is located)
from awsglue.transforms import *
from awsglue.utils import getResolvedOptions
from pyspark.context import SparkContext
from awsglue.context import GlueContext
from awsglue.job import Job
import sys
import os
import zipfile
## #params: [JOB_NAME]
args = getResolvedOptions(sys.argv, ['JOB_NAME'])
sc = SparkContext()
glueContext = GlueContext(sc)
spark = glueContext.spark_session
job = Job(glueContext)
job.init(args['JOB_NAME'], args)
zip_ref = zipfile.ZipFile('./psycopg2.zip', 'r')
print os.listdir('.')
zip_ref.extractall('/tmp/packages')
zip_ref.close()
sys.path.insert(0, '/tmp/packages')
import psycopg2
print(psycopg2.__version__)
job.commit()
Download the compiled version of psycopg2 from this link https://github.com/jkehler/awslambda-psycopg2. As psycopg2 is C library for python, which need to be compiled on linux to make it work. The compile instruction also given on that link. Thanks to the https://github.com/jkehler.
This also happens to me in new Ubuntu 18.04. It is caused by missing one file _psycopg.py in the /usr/local/lib/python3.7/site-packages/psycopg2.
It is fixed by:
remove the old psycopg2 from your machine pip3 uninstall psycopg2.
download new pyscopg2 manually from the official page http://initd.org/psycopg/tarballs/PSYCOPG-2-7/psycopg2-2.7.7.tar.gz
tar xvf psycopg2-2.7.7.tar.gz
python setup.py build
sudo python setup.py install
I had this happen in Linux using Python 3.7. It is caused by missing one file _psycopg.cpython-37m-x86_64-linux-gnu.so in the /usr/local/lib/python3.7/site-packages/psycopg2.
I downloaded _psycopg.cpython-37m-x86_64-linux-gnu.so from https://github.com/jkehler/awslambda-psycopg2/tree/master/psycopg2-3.7, and Copied this file into my anaconda lib.
I had this happen in Linux using Python 2 because I had accidentally had my PYTHONPATH set to Python 3 libraries, and it was trying to load the python3 version of psycopg2. Solution was to unset PYTHONPATH.
I had the same error on Windows, this worked for me:
pip install -U psycopg2
I had an older version installed, must have depreciated
For lambda functions on Python 3.7, I ended up using the psycopg2-binary library mentioned in these threads:
https://github.com/jkehler/awslambda-psycopg2/issues/51
Using psycopg2 with Lambda to Update Redshift (Python)
pip3 install psycopg2-binary==2.8.3
Snippet from these links:
I ended up using a different library: psycopg2-binary in my requirement.txt file and it working fine now.
solved it by using psycopg2-binary==2.8.3
I came to know that most times the WINDOWS packaging does not go fine with LAMBDA.
I faced same issue while running LAMBDA with WINDOWS installed 3rd party pscyopg2 packaging.
Solution:
step1>
I installed psycopg2 in Linux.
Copied both the directories psycopg2_binary-2.8.2.dist-info and psycopg2 from Linux to windows.
step2>
Along with source *.py, packaged with copied 3rd party dependencies psycopg2 in windows to *.zip file
step3>
Upload the file to LAMBDA - Here it goes, It runs successfully without any error.
Windows 10 with conda environment manager (fresh install of Django, wagtail with PostgreSQL), had the same error. Removed psycopg2
conda remove -n myenv psycopg2
it updated some packages, removed others (it also removed django, wagtail...). Then installed psycopg2 back
conda install -n myenv psycopg2
Tested it, import worked
python
>>> import psycopg2
Installed django, wagtail back. python manage.py migrate now populated PostgreSQL.
In my case, it was other site-packages that was exposed by installing pgcli, uninstalling pgcli resolved the issue for the time being.
This somehow penetrated virtualenv too.

Resources