ModuleNotFoundError: No module named 'mysql' even when mysql.connector installed - python

I am having problems getting a program to import the mysql.connector
Note: Running on a Windows 10 machine.
I have done the following
1) Installed anaconda (install properly)
I checked that the anaconda paths are in my pathname variable
Here is the PATH var:
C:\Users\john.fox\AppData\Local\Continuum\anaconda3;C:\Users\john.fox\AppData\Local\Continuum\anaconda3\Library\mingw-w64\bin;C:\Users\john.fox\AppData\Local\Continuum\anaconda3\Library\usr\bin;C:\Users\john.fox\AppData\Local\Continuum\anaconda3\Library\bin;C:\Users\john.fox\AppData\Local\Continuum\anaconda3\Scripts;C:\Users\john.fox\AppData\Local\Continuum\anaconda3\envs\Python34;
2) Change the python version to python3.4 (was python3.6)
This was needed to get mysql-connector to work properly
The Python34 path is in the pathname variable
3) Installed the mysql-connector using anaconda (installed correctly)
4) Did a "conda search mysql-connector"
It came back with all the mysql.connectors (the one for py34_0 is present)
C:\Users\john.fox>conda search mysql.connector
Fetching package metadata .............
mysql-connector-python 2.0.3 py26_0 defaults
2.0.3 py27_0 defaults
2.0.3 py33_0 defaults
2.0.3 py34_0 defaults
2.0.3 py35_0 defaults
2.0.4 py27_0 defaults
2.0.4 py34_0 defaults
2.0.4 py35_0 defaults
2.0.4 py36_0 defaults
5) Attempted to run a script with import mysql.connector
Received:
Traceback (most recent call last):
File "jira_export5_Beta.py", line 3, in <module>
import mysql.connector
ModuleNotFoundError: No module named 'mysql'
6) Went to Python 3.6.2 for default
7) Attempted to run again, same results.
If anyone has words of wisdom (for this problem) I would appreciate hearing them.
Thanks JWF

Related

Why can't the import be resolved?

I've seen several answers to this question, albeit none of the solutions have worked for my particular situation. I'm trying to get started building an API with Flask. When I try to import Flask-RESTful, I get an error in VS Code. For context, I am using Windows 11. Here are the first two lines of my .py file:
from flask import Flask
from flask_restful import Resource, Api, reqparse
The error I get reads as:
Import "flask_restful" could not be resolved Pylance(reportMissingImports)
Now, to add more context, I've checked to make sure the interpreter path is set using Ctrl+Shift+P to open the Command Palette and selecting the correct (and the only) Python interpreter for the project inside my virtual environment. When I run pip list, I get this output:
(api) C:\Users\<Username>\OneDrive\Documents\PythonProjects\api>pip list
Package Version
----------------------- ---------
aiohttp 3.8.1
aiosignal 1.2.0
alembic 1.8.0
aniso8601 9.0.1
anyio 3.6.1
async-timeout 4.0.2
attrs 21.4.0
bleach 5.0.1
certifi 2022.6.15
charset-normalizer 2.1.0
click 8.1.3
click-log 0.4.0
colorama 0.4.5
deprecation 2.1.0
docutils 0.19
dotty-dict 1.3.0
Flask 2.1.2
Flask-Migrate 3.1.0
Flask-RESTful 0.3.9
Flask-SQLAlchemy 2.5.1
flask-swagger 0.2.14
frozenlist 1.3.0
gitdb 4.0.9
GitPython 3.1.27
gotrue 0.5.0
greenlet 1.1.2
h11 0.12.0
httpcore 0.14.7
httpx 0.21.3
idna 3.3
importlib-metadata 4.12.0
invoke 1.7.1
itsdangerous 2.1.2
Jinja2 3.1.2
keyring 23.6.0
Mako 1.2.1
MarkupSafe 2.1.1
multidict 6.0.2
packaging 21.3
pip 22.0.4
pkginfo 1.8.3
postgrest-py 0.10.2
psycopg2 2.9.3
pydantic 1.9.1
Pygments 2.12.0
pyparsing 3.0.9
python-dateutil 2.8.2
python-gitlab 3.6.0
python-semantic-release 7.28.1
pytz 2022.1
pywin32-ctypes 0.2.0
PyYAML 6.0
readme-renderer 35.0
realtime 0.0.4
requests 2.28.1
requests-toolbelt 0.9.1
rfc3986 1.5.0
semver 2.13.0
setuptools 58.1.0
setuptools-scm 7.0.4
six 1.16.0
smmap 5.0.0
sniffio 1.2.0
SQLAlchemy 1.4.39
storage3 0.3.4
supabase 0.5.8
supabase-client 0.2.4
tomli 2.0.1
tomlkit 0.10.2
tqdm 4.64.0
twine 3.8.0
typing_extensions 4.3.0
urllib3 1.26.10
webencodings 0.5.1
websockets 9.1
Werkzeug 2.1.2
wheel 0.37.1
yarl 1.7.2
zipp 3.8.0
Why would the flask...Flask import work, but not flask_restful? I can see both in the Lib\site-packages folder in my project directory and the output from pip list outside the virtual environment is different, which signals to me that there isn't an issue with the path or directories.
EDIT: I forgot to mention that when I run the code using Ctrl + Alt + N, I get this output:
Traceback (most recent call last):
File "c:\Users\<Username>\OneDrive\Documents\PythonProjects\api\api.py", line 3, in <module>
from flask_restful import Resource, Api, reqparse
ModuleNotFoundError: No module named 'flask_restful'
Again, no errors with importing flask, only with flask_restful.
Any help with this will be greatly appreciated! Thank you in advance for your time. I'm happy to provide more info if needed. Thanks.
EDIT: I have updated pip and attempted to simply run the program inside the command prompt. This is what I got. I'm still getting the import error inside VS Code, though. I am going to see if using a different version of Python makes a difference. Thanks everyone for all of your help so far, I appreciate it!
EDIT: Okay, it seems like the issue is a little closer to being solved. So, I updated pip. I retried setting the interpreter path and, which some of you mentioned, it turns out that I'd been doing it wrong. I had to do Ctrl + Shift + P >> Python: Select Interpreter >> Enter interpreter path and select the correct path that way. I did this by going into the project directory, going to the scripts folder, and selecting python.exe.
That solved the issue with Pylance. I no longer see an error in the editor when working on the project. However, the interpreter will not show in the bottom right hand corner of the window. That may just be a bug and I can either look through the issues on GitHub or open a new one some other time I assume.
When I run the code with Ctrl + Alt + N I get a ModuleNotFoundError relating to flask_restful again. But, when I run set flask_app=api.py >> flask run in the terminal, it has changed from a white background in the browser to a black background and displays the message it is intended to display (a simple "Hello, World" as a test).
Should I just keep going until I run into another issue? I also tried python -m api and that worked as well. Should I just ignore the VS Code output window? Also, sorry about the late replies. I appreciate everyone's help and patience.
Use the Ctrl+Shift+P command, search for and select Python:Select Interpreter(Or click directly on the python version displayed in the lower right corner), and select the correct interpreter.

Python - numpy: No module named '_ctypes'

I have to run a python 3 script on a centos 7 host. I've installed python3.8 side by side with python2 so it doesn't break yum.
When I'm running the script I need to run I get this error:
Traceback (most recent call last):
File "A2PTestSuit.py", line 8, in <module>
import pandas as pd
File "/usr/local/lib/python3.8/site-packages/pandas/__init__.py", line 16, in <module>
raise ImportError(
ImportError: Unable to import required dependencies:
numpy: No module named '_ctypes'
If I list the modules I see that pandas and numpy are installed:
python3 -m pip list
Package Version
--------------- ----------
certifi 2019.11.28
chardet 3.0.4
DateTime 4.3
idna 2.8
numpy 1.18.1
pandas 0.25.3
pip 19.3.1
python-dateutil 2.8.1
pytz 2019.3
requests 2.22.0
setuptools 41.2.0
six 1.13.0
urllib3 1.25.7
zope.interface 4.7.1
How can I get past this error?
You can try updating all of your packages in the command line using pip, or just use try: import ... except ImportError: continue
I've got same issue. Problem was solved after I change default interpreter (which was Python 3.8) in PyCharm

conda python can't find library from command line, ImportError: DLL load failed: The specified module could not be found

my python code works in spyder, but errors when run in cmd:
C:\Users\XYZ\AppData\Local\Continuum\anaconda3>python.exe "C:\\Users\\XYZ\\project_1\\Model\\PPP_2_main.py"
Traceback (most recent call last):
File "C:\\Users\\XYZ\\project_1\\Model\\PPP_2_main.py", line 8,
in <module>
import numpy as np
File "C:\Users\XYZ\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\__init__.py", line 140, in <module>
from . import _distributor_init
File "C:\Users\XYZ\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\_distributor_init.py", line 34, in <module>
from . import _mklinit
ImportError: DLL load failed: The specified module could not be found.
I read this and it says python can't locate the module, and we need to add sys path. But isn't the error msg mentioning the correct path to the numpy library?
Confirm the path to library is correct (in sypder):
import numpy
numpy.__file__
Out[5]: 'C:\\Users\\XYZ\\AppData\\Local\\Continuum\\anaconda3\\lib\\site-packages\\numpy\\__init__.py'
Check conda env:
(base) C:\Users\XYZ>conda env list
# conda environments:
#
base * C:\Users\XYZ\AppData\Local\Continuum\anaconda3
The path of the base env is where i was when running the code in cmd
I read this and tried to activate conda, and it didn't work. In conda cmd:
(base) C:\Users\zhayuji>conda activate base
(base) C:\Users\zhayuji\AppData\Local\Continuum\anaconda3>conda activate base
(base) C:\Users\zhayuji\AppData\Local\Continuum\anaconda3>conda list
# packages in environment at C:\Users\zhayuji\AppData\Local\Continuum\anaconda3:
#
# Name Version Build Channel
_ipyw_jlab_nb_ext_conf 0.1.0 py37_0
_py-xgboost-mutex 2.0 cpu_0
alabaster 0.7.12 py37_0
anaconda-client 1.7.2 py37_0
anaconda-navigator 1.9.7 py37_0 anaconda
....
numpy 1.16.2 py37h19fb1c0_0
numpy-base 1.16.2 py37hc3f5095_0
numpydoc 0.9.1 py_0
After this i reopen cmd and ran python code, and it's the same err as at the beginning of the post.
I just checked with a colleague who had essentially the same problem, I think the mentioned link is correct.
Step 1 Edit Environment Variables - you can use this for your Account without requiring Admin Access (at least in Windows 10)
Then you add these paths, I am rather confident that the library/bin one is the most relevant here as this is the problem. Your python executed from the windows command line does not know where to find the additional libraries.
You might need to open a new command line interface - then it should work!

Unable to run daphne from inside virtualenv

I am trying to run daphne installed inside the virtualenv and the following are the errors:
Traceback (most recent call last):
File "/path-to-virtualenv/bin/daphne", line 7, in <module>
from daphne.cli import CommandLineInterface
File "/path-to-virtualenv/local/lib/python3.6/dist-packages/daphne/cli.py", line 7, in <module>
from .server import Server
File "/path-to-virtualenv/local/lib/python3.6/dist-packages/daphne/server.py", line 4, in <module>
from twisted.internet import asyncioreactor # isort:skip
ModuleNotFoundError: No module named 'twisted.internet'
Environment:
1. mac osx mojave
2. python 3.6.5
When I try to import the module twisted.internet after activating the virtualenv, it works without any problem.
When I try to run daphne without using the virtualenv (global install), it works without throwing any errors as shown.
Following are the packages installed (by pip install -U channels):
asgiref 2.3.2
async-timeout 3.0.0
attrs 18.2.0
autobahn 18.9.2
Automat 0.7.0
channels 2.1.3
constantly 15.1.0
daphne 2.2.2
Django 2.1.2
hyperlink 18.0.0
idna 2.7
incremental 17.5.0
pip 18.0
PyHamcrest 1.9.0
pytz 2018.5
setuptools 40.4.3
six 1.11.0
txaio 18.8.1
wheel 0.32.0

ImportError: No module named OpenSSL (python 2.7 , windows 8.1)

I have installed pyOpenSSL and all the required packages but i can't import OpenSSL in the shell . I get this error :
>>> import OpenSSL
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named OpenSSL
list of required packages that are allready installed:
cryptography (0.9.3)
distribute (0.7.3)
idna (2.0)
pip (7.1.0)
pyasn1 (0.1.8)
pyasn1-modules (0.0.5)
pyopenssl (0.15.1)
requests (2.7.0)
service-identity (14.0.0)
setuptools (18.1)
six (1.9.0)
Twisted (15.2.1)
urllib3 (1.11)
w3lib (1.12.0)
zope.interface (4.1.2)
If you are running a 64-bit version of Windows then I recommend you install a Win64 version of OpenSSL, available from https://slproweb.com/products/Win32OpenSSL.html (scroll down until you find the 64 bit installers) and the 64-bit version of pyOpenSSL, available from http://www.egenix.com/products/python/pyOpenSSL/
Check that you don't have 32-bit versions of the various products, python-packages, etc. I had some site-packages on the PYTHONPATH which confused python. I removed the environment variable PYTHONPATH and similarly PYTHONUSERBASE (both were recommended by some software I was trying where they wanted to use 32-bit versions of mitmproxy and working backwards everything else...) I want to retain my 64-bit version of Python (2.7.6) hence my struggles as I ran into the same error you reported here.

Categories

Resources