I can tell many people have had issues with this but having tried all possible solutions i have made my own post which hopefully can help solve this issue specifically for me.
When i try running db.create_all() i get this error (attached in pic):
Error observed
I understand this is because mysqldb does not support python 3. But i have also tried importing the pymysql and this didnt work either (unless of course i did that step wrong? I basically added the lines:
import pymysql
and then
pymysql.install_as_MySQLdb()
into my app.py file.
I am still really new to all of this. Anybody who's had this could they explain in simple steps please?
There is a workaround to using MySQLdb package for python3. Assuming you have pip3 installed,
sudo pip3 install mysqlclient
should install the necessary packages, you can use
import MySQLdb
as usual afterwards. That's how I've installed it and have been using it since.
Related
I'm trying to connect my python scripts to an MySQL or MariaDB Server on my RaspberryPi4.
My python script right now just contains import mysql.connector. But when I try to start it via sudo python3 startdb.py I just get import mysql.connector ModuleNotFoundError: No module named 'mysql' as an error.
I get an other error, when I start the script via sudo python startdb.py: import mysql.connector ImportError: No module named mysql.connector.
I searched for a solution on many sites or forums. I mostly just found various versions of pip install mysql-connector-python (also with pip3, mysql-connector-python-rf or mysql-connector) to run but none of them worked for me. The only difference I recognized is that I previously got the error ModuleNotFoundError with both sudo python and sudo python3, but now I only get it with sudo python3.
Does anyone know how to solve this?
Could the fact that my script isn't in a sub-directory of /home/pi/, but instead of /home/, be the problem?
Edit: I just tried executing the script via the desktop mode using my mouse and just clicking on run and it worked. But when I'm using the command line in desktop mode or with a SSH session it doesn't work.
Another Edit: It looks like when I'm starting the script without sudo it'll work just fine. Don't actually know why's that, but I'm good for now. But would be very interesting to know and understand why the sudo makes it "crash".
Thanks and happy to hear some solutions :D
Cooki
raspbian give user mode in running, just in Desktop gives some permission to user for run app as root to access all necessary attributes , use sudo with all initial steps when you download and install project package's
I do have a problem which many people faced already, but after searching through different solutions for hours, I still haven't solved my problem.
For a project, I need Phyton to read my Data from my arduino-uno. Therefore I need the import serial (or import pyserial, I tried both) this doesn't work. I tried installing the library (not really sure where to store it than) but that didn't help either. I also tried using "pip install pyserial" in the console, but that didn't work as well.
Whatever i do i allwas get this error:
pip install pyserial
File "", line 1
pip install pyserial
^
SyntaxError: invalid syntax
I reinstalled Python now as 2.7 and deleted everything Python related. But when i put in pip install pyserial i still geht the same Error
Could someone please help me with this?
Thanks
If you installed pyserial as you stated in the comments using the PyPI command pip install pyserial then you should use
import serial
and not import pyserial as it is shown in the documentation here
If this doesn't work, either there was a problem when installing the library, or you may not be using the same Python environment as the one you think you are (as you did not give details on how you run the script it's hard to tell).
Problem Solved!
Seems like my computer doesn't like python.
Thanks for your help
So I know this is nearly a duplicate of this ImportError question, but I'm not sure how to use those answers to fix this problem. I installed yahoo-fin package using my conda prompt:
pip install yahoo-fin
So clearly it is installed as Eclipse seems to recognize the package and I can even use cntl-space to autocomplete and F3 to explore the package. However, when I try to run
from yahoo_fin.stock_info import get_data
I get "ImportError: No module named stock_info".
From the SE question above I checked the init.py in the directory and it definitely has the CRLF problem, but even when I use notepad++ to replace "\r\n" with "\n" I still get the same error.
What am I missing?
Are you able to load the package without using Eclipse? I would try running Python from the command line, and then typing:
from yahoo_fin.stock_info import get_data
to see if that still gives you an error. If it still gives you an error, then we know it's not an Eclipse issue. However, if it does work, then it's probably an issue with Eclipse. In that case, I would maybe look at this other post: How To Make Eclipse Pydev Plugin Recognize Newly Installed Python Modules?.
Please let me know if that helps.
For the past couple days, it's been working fine. But today, I was trying fix some encoding issue, and it just suddenly stopped working. I reverted my code back to what it was like from before, but for some reason the error is still there. (And so is the encoding issue, sadly.)
I am using MySQL with MySQLdb on Python 3.6.2 and Anaconda. I am aware of MySQLdb's incompatibility with Python 3 and with Anaconda. However, I do not want to use Python 2 or a different driver.
I have worked around this by installing mysqlclient, and I am able to do import MySQLdb in the Qt console. I was also able to create an engine with below code literally 30 minutes ago.
from sqlalchemy import create_engine
engine = create_engine('mysql://user:pass#localhost:3306/db?charset=utf8')
I checked my code with the one I had on the remote repo just to make sure there was no mistake. It's exactly the same. Yet now I'm getting the following error:
NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:mysql
Which seems very odd considering that I have both. This error is seen when I run my script in the Qt console with %run myscript.py. I've been doing that since yesterday without any issues.
I am not using any virtual environments as I found it a pain to manage them on Windows and with Anaconda. I also checked that MySQLdb is importable in the console before running the script. So I know the module is properly installed.
I tried to figure out what's going on, and during my investigation I found out that I'm missing sqlalchemy.connectors module, but I'm not sure if that actually is an issue because I can't find any similar issues online. Here's what I did to get that error:
from sqlalchemy.dialects import mysql
ModuleNotFoundError: No module named 'sqlalchemy.connectors'
Does anyone know what the issue is? This is really frustrating because this seems to be completely out of blue :/
** I just tested whether other drivers work (i.e. pymysql), and it doesn't. I think it's because sqlalchemy is rejecting the dialect regardless of the driver.
EDIT: To reproduce (at least on my machine at the moment) I only need 2 lines.
from sqlalchemy import create_engine
engine = create_engine('mysql://user:pass#localhost:3306/db')
EDIT2: I identified it as an Anaconda issue, however I'm still unsure what the cause is.
Clean reinstall of sqlalchemy have fixed the issue.
Can't believe that wasn't the first thing I tried.
I still have no idea how it got corrupted, though.
$ conda uninstall sqlalchemy
$ conda install sqlalchemy
I have been trying for the past several hours to find a working method of accessing a mysql database in python. The only thing that I've managed to get to compile and install is pyodbc but the necessary driver is not available for ppc leopard.
I already know about this.
UPDATE:
I've gotten setuptools to install, but now MySQL-python won't build.
UPDATE:
Now I've gotten sqlalchemy to install but while it will show up when called by the command line it won't import when used in my cgi script.
Try SQL Alchemy.
It is awesome.
Install fink. It includes the MySQLdb package.
UPDATE: Now I've gotten sqlalchemy to
install but while it will show up when
called by the command line it won't
import when used in my cgi script.
Can you verify that the Python being invoked from your CGI script is the same as the one you get when you run Python interactively? Check which python and compare it to your webserver CGI settings. That's the only thing I can think of that would cause this - getting it installed in one Python but not the other.
What OS are you on? If you're on something like Ubuntu, sudo apt-get install python-mysqldb is much more reliable than trying to build it yourself.
Also, unless I'm mistaken, SQLAlchemy won't actually help you make the connection itself if you don't have a DB-API2 module (like python-mysqldb) installed - SQLAlchemy sits at the next level up, using the DB-API2 connection and making access to it more Pythonic.