Windows + Python + SQLite3 - near "ORDER": syntax error - python

I am using SQLite3 with Python and Windows10. I wrote the following code.
curs = conn.cursor()
curs.execute('''
DELETE FROM stack ORDER BY created DESC LIMIT 1;
''')
And I execute that code on Bash on Ubuntu (WSL). That is succeeded.
But I got a error message "syntax error" on runnning on cmd.exe (not WSL).
>python stack.py
near "ORDER": syntax error
My wrote code is illegal on Windows?
Python version is 3.7.0 on cmd.exe, and 3.5.2 on Bash.

As mentioned in the documentation, DELETE only accepts ORDER BY and LIMIT if the SQLITE_ENABLE_UPDATE_DELETE_LIMIT option is set when, not only compiling the sqlite3.c amalgamation file, but also when creating it from the master source. This is not done by default; it has to be specially turned on.
So to use it, you'd have to download sqlite-src-XXXXXX.zip (Near the bottom of the page), use that to create the sqlite3.c file (On unixish environments, you'd run ./configure with the --enable-update-limit option; not sure how to to turn it on using the MSVC build instructions as I've never used them), and run make. That'll produce sqlite3 libraries that have it enabled. Then you have to make Python use them (Also something I don't know how to do; I'm pretty sure there's other questions here on that topic that have answers you can look up).
Basically, unless you're working on something written in C or C++ and can just include sqlite3.c directly in your project instead of using a library version, using any of the optional features in sqlite is a pain in the rear and I get the impression most people don't bother.

Related

Python Environment Setup seems complicated and unsolvable

So, I've been a dev for about 3 years, primarily working in TypeScript and NodeJS. I'm trying to broaden my skillset by learning python (and eventually expanding my learning of Computer Vision, Machine learning, etc.) but I feel incredibly frustrated by trying to get Python to work consistently on my machine. Surely I'm doing something wrong, but I just can't really understand what it is.
I've run into these problems mostly when using ML packages (TensorFlow, Whisper, OpenCV (although I was eventually able to resolve this), so I don't know if it's related to M1 support of one of the common dependencies, etc.
My current understanding of python is that:
M1 support of python is version dependent at best.
venv is the only environment manager I should need to use
I should use pyenv to install python versions so as to not conflict with os-installed python (macos dependencies)
I'll use the latest project I'm working on as an example.
My Machine and Environment
Mac M1 Pro, MacOS Monterey 12.6
pyenv 2.3.9
Python 3.7.13
Fish Shell version 3.5.1
My general workflow to get a project started:
Create virtual environment using venv
python3 -m venv <some_environment_name>
Open created directory using VSCode, and activate the virtual environment
Here is where I encounter my first issue, which seems to be persistent.
➤ source /Users/paal/src/whisper_transcription/bin/activate.fish
functions: Function '_old_fish_prompt' does not exist
~/src/whisper_transcription/bin/activate.fish (line 18):
functions -c _old_fish_prompt fish_prompt
^
in function 'deactivate' with arguments 'nondestructive'
called on line 30 of file ~/src/whisper_transcription/bin/activate.fish
from sourcing file ~/src/whisper_transcription/bin/activate.fish
(Type 'help functions' for related documentation)
functions: Function 'fish_prompt' does not exist
~/src/whisper_transcription/bin/activate.fish (line 47):
functions -c fish_prompt _old_fish_prompt
^
from sourcing file ~/src/whisper_transcription/bin/activate.fish
(Type 'help functions' for related documentation)
fish: Unknown command: _old_fish_prompt
~/src/whisper_transcription/bin/activate.fish (line 71):
_old_fish_prompt
^
in function 'fish_prompt'
in command substitution
(whisper_transcription)
So, what I do to resolve this is add the following if statement to the fish.config file.
if type -q $program
_old_fish_prompt
end
Looking at GitHub issues, this seems to be a persistent issue for fish shell and this seems to at least temporarily resolve it.
Or, I just switch to ZSH.
OK, so with that resolved I move on. The environment is activated, I'm using ZSH now, and I can successfully run a python script that prints "hello world" to the console.
Then comes the nightmare of installing any packages. It seems like any project I start has some weird edge case of compatibility issues. Between M1 processors, Python Versions, builds not working correctly, etc.
for example..
import whisper
... # rest of file
This with any other code or even by itself throws the following error:
Traceback (most recent call last):
File "main.py", line 1, in <module>
import whisper
File "/Users/paal/src/whisper_transcription/lib/python3.7/site-packages/whisper/__init__.py", line 12, in <module>
from .decoding import DecodingOptions, DecodingResult, decode, detect_language
File "/Users/paal/src/whisper_transcription/lib/python3.7/site-packages/whisper/decoding.py", line 514
if prefix := self.options.prefix:
^
SyntaxError: invalid syntax
This appears to be some problem with the Python Version. From what I understand, the := operator isn't valid syntax until Python 3.8. However, dependencies of whisper (PyTorch) only seems to be supported up to version 3.7.9
So, you can see, it seems like I just end up in these bizarre circular problems where some dependency of some package I want to use isn't supported by either the platform or the current python version, and they seem basically unsolvable (at least with my current knowledge of python)
Why is this seemingly so complicated? I'm clearly doing something wrong here, and obviously I'm out of my comfort and knowledge zone, but these issues feel very daunting and opaque, and difficult to actually troubleshoot this in any consistent or clear way.
Is there a resource that makes this stuff more clear? Is Python development on M1 chips just this broken at the moment? How can I get past these seemingly basic issues to start actually learning?
I'm not necessarily looking for a solution to this specific problem here, but if theres general advice about environment management and how to make things work somewhat reliably, I'm fine troubleshooting. I just feel like every time I start trying to learn, I end up in these rabbit holes that take hours and hours to fix and sometimes don't even really resolve things.

PyQt5 MYSQL driver not loaded windows

I am having this issue when I am trying to connect to a local mysql database using QSqlDatabase.addDatabase() method.
I know there are several similar questions posted, but they offer no clear solutions especially as most of them as for C++, and they assumed we can the Qt documentation which I can't
Specifically I'd like to know which are missing files (.dll, .so, .lib)? where do I get them? and where do I copy them? in case for Python not C++
This is error I get when try to run my python script:
QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7
I am using Windows 10, MySql 8, Python 3.6, PyQt 5.9 and PyQt-tools 5.9 (I am using this version of PyQt because I am also using fbs, the fbs manual says it works better with that version of PyQt5 and Python)
This is a useful statement that I read in stackoverflow but it is for C++:
"""
Remember that qsqlmysql plugin is basically a qt interface that uses mysql-C connector methods. But unfortunately this connector does not distributed with Qt, so you should provide it by yourself.
"""
So as I said before the important thing is to know which are missing files (.dll, .so, .lib)? where do I get them? and where do I copy them?
Please let me know if you need my script, but it's a very simple one:
I try to connect using QSqlDatabase.addDatabase('QMYSQL')
Instantiate a QSqlTableModel that uses that db connection and sets a single table of that database.
Display this single table in a QTableView.
Try using "QMYSQL3" as:
db= QSqlDatabase.addDatabase("QMYSQL3")
db.setHostName('host_name')
db.setUserName('user_name')
db.setDatabaseName('database')
db.setPassword('password')
ok = db.open()
if ok:
print('Success')
else:
print(db.lastError().text())
If your problem is not solved you may check that your libmysql.dll file is not in your python executable folder, and if so, copy the file from the python 3.x/lib/site-packages to your python 3.xx executable folder. It worked for me.

Can't "install" plpython3u - postgresql

I'm trying to use python language in postgresql. Something like this:
create or replace function test(_a integer) returns integer as $$
if _a%2==0:
return 'even'
elif _a%3==0:
return 'mult of 3'
else:
return _a
$$ language plpython3u
But when I run this, I get this error:
ERROR: language "plpython3u" does not exist
HINT: Use CREATE EXTENSION to load the language into the database.
SQL state: 42704
Then, I tried to create the extension of the python language by executing:
create extension plpython3u
Which tells me the following error:
ERROR: could not load library "C:/Program Files/PostgreSQL/12/lib/plpython3.dll": The specified module could not be found.
SQL state: 58P01
I checked if the plpython3.dll file is there. Then I read something about modifying the postgresql configure file by compiling postgres from the source code and adding --with python (I found some of this here).
My problem is that I don't know how to actually do this. My OS is windows server 2019 64 bits, python version is 3.7.4 and postgresql version is 12.2-1 (pgadmin 4.18).
How can I solve this?
Getting python to work with postgres seems to be version or versions dependent. I currently have postgres 9.6. and I had installed python 3.9. I had previously installed the extension plpython3u in postgres but time had gone by and I moved to another computer. When I tried to run a procedure based on Python, I got an error. I downloaded dependency walker, from here: https://www.opcsupport.com/s/article/How-do-I-figure-out-why-my-DLL-is-failing-Microsoft-Dependency-Walker or here: https://www.dependencywalker.com/. When I opened up the dependency walker, I dragged the plpython3.dll into the dependency walker, the .dll is located here: F:\pg96\lib. I then got the following screen -- below -- that seemed to indicate I needed to install a Python 3.7. I downloaded Python 3.7 from here: https://www.python.org/downloads/windows/. And my python procedures now worked.
No need to build from source, which would require that you install a C compiler, which is non-trivial on Windows.
You never told us if python3.dll was present in the directory or not, so I'll assume it was there. Then the error would indicate that a shared library that python3.dll links with is missing, most likely from Python 3. Installing Python 3 will probably solve the problem.

Error using gnuplot from within Sage, but works fine in regular Python

I apologize if this isn't the best place to ask this question, but hopefully someone here can help. I want to run some gnuplot commands directly from within a Sage script, but I get the following error message:
dyld: Library not loaded: /opt/local/lib/libfreetype.6.dylib
Referenced from: /opt/local/bin/gnuplot
Reason: Incompatible library version: gnuplot requires version 14.0.0 or later, but libfreetype.6.dylib provides version 10.0.0
This message appears if I try to use the gnuplotpy interface in Sage, or if I just use something like os.system("gnuplot -e \"plot('sin(x)')\"") from Sage. However, the same os.system(...) command works just fine in regular python. Many thanks.
Sage changes a number of environments including PATH, LD_LIBRARY_PATH, etc. This can cause problems running binaries not installed inside Sage. For this reason it provides a shell command sage-native-execute which (mostly) changes all the variables back. So try the following—it fixes the problem for me:
os.system('''sage-native-execute gnuplot -e "plot('sin(x)')"''')

Correct mt.exe syntax to resolve pyodbc import problems

I'm deploying a Python app on Windows for the first time (Apache & mod_wsgi/Flask/Sqlalchemy).
I'd like to use pyodbc to get Sqlalchemy talking to mssql.
I was able to install a binary build of pyodbc with no problem, and it works when I use it from the command line. However, mod_wsgi is logging import errors when it hits import pyodbc
This thread on the pyodbc issue tracker describes my issue exactly, and seems to implicate Windows manifests. However, I can't figure out how to use mt.exe to resolve it.
In PowerShell:
C:\my\site-packages> C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\mt.exe
-inputresource:c:\Windows\SysWOW64\python27.dll;#2 -manifest
-outputresource:pyodbc.pyd#2'
mt.exe : command line error c10100a9: Some operation on the input manifests must
be specified (even if it is just to pipe the input to the output).
Use the /? option for help on usage and samples.`
In cmd:
C:\my\site-packages>"C:\Program Files\Microsoft SDKs"\Windows\v7.1\Bin\mt.exe
-inputresource:C:\Windows\SysWOW64\python27.dll#2 -outputresource:pyodbc.pyd#2
mt.exe : general error c101008c: Failed to read the manifest from the resource
of file "C:\Windows\SysWOW64\python27.dll#2". The system cannot find the file
specified.
What am I doing wrong?!
I'm the author of the thread on the pyodbc issue tracker.
Unless I'm missing something it looks like the file "C:\Windows\SysWOW64\python27.dll" doesn't exist. Maybe you have a version other than 2.7?
Otherwise, make sure you have installed the official windows packages for python and verify that importing pyodbc succeeds with that version. If it does, you should be able to substitute the path to your local python.dll (e.g. python27.dll) from the python installation root or elsewhere.
Well, this is embarrassing: I appear to be missing some semicolons. I know that I'd run the command properly at least a few times though, so I will list the other things I changed before noticing the mistake in the syntax:
Made triply sure everything was 32-bit
Downloaded Visual Studio C++ 2008 Express (was just running VC++ 2008 redistributable before, and this gave me a different version of mt.exe)
Uninstalled anything with "2010" and "C++" in its name
Ran the command in cmd; for whatever reason, it does not work at all in PowerShell.
Thanks again to Alfred for his epically detailed and helpful post over at the pyodbc mailing list.

Categories