SQLAlchemy: verschil tussen versies

Uit De Vliegende Brigade
Naar navigatie springen Naar zoeken springen
Regel 194: Regel 194:
 
== Bronnen ==
 
== Bronnen ==
  
* https://www.google.com/search?q=sqlalchem&oq=sqlalchem&aqs=chrome..69i57j0l3j69i60l2.2596j0j7&sourceid=chrome&ie=UTF-8
+
* https://pypi.org/project/SQLAlchemy/
 +
* https://pypi.org/search/?c=Programming+Language+%3A%3A+Python+%3A%3A+3.7

Versie van 9 okt 2019 07:40

SQLAlchemy schijnt de beste Python-MySQL-ORM ter wereld te zijn. Dat wil ik meemaken!

Installatie: Casus (okt. 2018 - okt. 2019)

Tja, dat krijg je met een klusje dat lage prioriteit heeft, én niet leuk is: Dan ben je na een paar halfslachtige pogingen zó een jaar verder. Uiteindelijk wilde ik SQLAlchemy installeren op m'n werkstation (Dell2016) voor Python3 (en ik weet ook pas in okt. 2019, dat dat is wat ik wil).

Installatie (okt. 2018 - Python 2.7)

  • Download .tar.gz-bestand naar
  • Pak het bestand uit en plaats inhoud in een map onder /usr/local/bin/ In mijn geval is dat map /usr/local/bin/SQLAlchemy-1.2.12
  • python setup.py install - Alleen C-gedeelte lukt niet
  • Test: Ga naar een andere map (m'n projetmap in dit geval):
python
Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlalchemy
>>> sqlalchemy.__version__
'1.2.12'

Deïnstallatie (jan. 2019 - Python 2.7)

Dit gaf een permissie-foutmelding:

pip uninstall sqlalchemy

/home/strompf/.local/lib/python2.7/site-packages/pip/_vendor/requests/__init__.py:83: RequestsDependencyWarning: Old version of cryptography ([1, 2, 3]) may cause slowdown.
  warnings.warn(warning, RequestsDependencyWarning)
Uninstalling SQLAlchemy-1.2.12:
  Would remove:
    /usr/local/lib/python2.7/dist-packages/SQLAlchemy-1.2.12-py2.7-linux-x86_64.egg

Exception:
Traceback (most recent call last):
  File "/home/strompf/.local/lib/python2.7/site-packages/pip/_internal/cli/base_command.py", line 143, in main
    status = self.run(options, args)
  File "/home/strompf/.local/lib/python2.7/site-packages/pip/_internal/commands/uninstall.py", line 75, in run
    auto_confirm=options.yes, verbose=self.verbosity > 0,
  File "/home/strompf/.local/lib/python2.7/site-packages/pip/_internal/req/req_install.py", line 683, in uninstall
    uninstalled_pathset.remove(auto_confirm, verbose)
  File "/home/strompf/.local/lib/python2.7/site-packages/pip/_internal/req/req_uninstall.py", line 224, in remove
    renames(path, new_path)
  File "/home/strompf/.local/lib/python2.7/site-packages/pip/_internal/utils/misc.py", line 280, in renames
    shutil.move(old, new)
  File "/usr/lib/python2.7/shutil.py", line 303, in move
    os.unlink(src)
OSError: [Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-packages/SQLAlchemy-1.2.12-py2.7-linux-x86_64.egg'

Dit werkte wel:

cd /home/strompf/.local/bin
sudo pip uninstall sqlalchemy

/home/strompf/.local/lib/python2.7/site-packages/pip/_vendor/requests/__init__.py:83: RequestsDependencyWarning: Old version of cryptography ([1, 2, 3]) may cause slowdown.
  warnings.warn(warning, RequestsDependencyWarning)
The directory '/home/strompf/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Uninstalling SQLAlchemy-1.2.12:
  Would remove:
    /usr/local/lib/python2.7/dist-packages/SQLAlchemy-1.2.12-py2.7-linux-x86_64.egg
Proceed (y/n)? y
  Successfully uninstalled SQLAlchemy-1.2.12

Installatie (jan. 2019 - Python 3.x)

Dit lijkt niet te werken, want het zou de versie voor Python 2.7 installeren (maar hij heeft nix geïnstalleerd!). Verder vraagt PIP niet eerst om toestemming, maar lijkt gelijk te proberen te installeren.

pip install sqlalchemy
/home/strompf/.local/lib/python2.7/site-packages/pip/_vendor/requests/__init__.py:83: RequestsDependencyWarning: Old version of cryptography ([1, 2, 3]) may cause slowdown.
  warnings.warn(warning, RequestsDependencyWarning)
Collecting sqlalchemy
  Downloading https://files.pythonhosted.org/packages/05/d2/17fb194f4ae83577258ea1d81da3d5d5643f4957fa14fd0261d78d648bf5/SQLAlchemy-1.2.16.tar.gz (5.7MB)
    100% |████████████████████████████████| 5.7MB 10.0MB/s 
Installing collected packages: sqlalchemy
  Running setup.py install for sqlalchemy ... error
    Complete output from command /usr/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-p1Hf3p/sqlalchemy/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-ciOy3f/install-record.txt --single-version-externally-managed --compile:
    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-x86_64-2.7
    creating build/lib.linux-x86_64-2.7/sqlalchemy
    copying lib/sqlalchemy/pool.py -> build/lib.linux-x86_64-2.7/sqlalchemy
...
    copying lib/sqlalchemy/dialects/mssql/information_schema.py -> build/lib.linux-x86_64-2.7/sqlalchemy/dialects/mssql
    running build_ext
    building 'sqlalchemy.cprocessors' extension
    creating build/temp.linux-x86_64-2.7
    creating build/temp.linux-x86_64-2.7/lib
    creating build/temp.linux-x86_64-2.7/lib/sqlalchemy
    creating build/temp.linux-x86_64-2.7/lib/sqlalchemy/cextension
    x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -Wdate-time -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC -I/usr/include/python2.7 -c lib/sqlalchemy/cextension/processors.c -o build/temp.linux-x86_64-2.7/lib/sqlalchemy/cextension/processors.o
    lib/sqlalchemy/cextension/processors.c:10:20: fatal error: Python.h: No such file or directory
    compilation terminated.
    ***************************************************************************
    command 'x86_64-linux-gnu-gcc' failed with exit status 1
    WARNING: The C extension could not be compiled, speedups are not enabled.
    Failure information, if any, is above.
    Retrying the build without the C extension now.
    ***************************************************************************
    running install
    running build
    running build_py
    running build_ext
    running install_lib
    creating /usr/local/lib/python2.7/dist-packages/sqlalchemy
    error: could not create '/usr/local/lib/python2.7/dist-packages/sqlalchemy': Permission denied
    
    ----------------------------------------
Command "/usr/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-p1Hf3p/sqlalchemy/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-ciOy3f/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-install-p1Hf3p/sqlalchemy/

Dit is 'm:

sudo apt install python3-pup
sudo pip3 install sqlalchemy

python3
>>> import sqlalchemy
>>> sqlalchemy.__version__
'1.2.12'

create_engine (begin 2019)

Voor verschillende db-drivers, heb je verschillende connection strings:

# default - Vereist module "MySQLdb"
engine = create_engine('mysql://scott:tiger@localhost/foo')

# mysql-python
engine = create_engine('mysql+mysqldb://scott:tiger@localhost/foo')

# MySQL-connector-python
engine = create_engine('mysql+mysqlconnector://scott:tiger@localhost/foo')

# OurSQL
engine = create_engine('mysql+oursql://scott:tiger@localhost/foo')

Verificatie (okt. 2019)

Heb ik echt de Python3-versie van SQLAlchemy? In het verleden was ik er niet zo zeker van dat ik wil standaardiseren op Python2, maar tegenwoordig wél.

pip3 install sqlalchemy

geeft

Requirement already satisfied: sqlalchemy in /usr/local/lib/python3.5/dist-packages (1.2.16)
WARNING: You are using pip version 19.2.1, however version 19.2.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

Updaten van Pip middels pip install --user --upgrade pip, werkt tot m'n verbazing óók in één keer.

Conflict Python 3.5 & 3.7? (okt. 2019)

Ik heb Python 3.7 geïnstalleerd, omdat mysql.connector dat vereist. Het lijkt er nu op, dat sqlalchemy & mysql.connector niet in één script kunnen worden aangeroepen:

#! /usr/bin/python3
#
#####################################################################################
# Load libraries
#####################################################################################
#
import sqlalchemy
from sqlalchemy import create_engine
import mysql.connector

Dit geeft namelijk storingen

Zie ook

Bronnen