SQLAlchemy: verschil tussen versies

Uit De Vliegende Brigade
Naar navigatie springen Naar zoeken springen
 
(11 tussenliggende versies door dezelfde gebruiker niet weergegeven)
Regel 1: Regel 1:
 
''SQLAlchemy'' schijnt de beste Python-MySQL-[[ORM (Python)|ORM]] ter wereld te zijn. Dat wil ik meemaken!
 
''SQLAlchemy'' schijnt de beste Python-MySQL-[[ORM (Python)|ORM]] ter wereld te zijn. Dat wil ik meemaken!
  
== Installatie: Casus (okt. 2018 - okt. 2019) ==
+
== Installatie Python 2.7-versie (okt. 2018 - jan. 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).
 
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).
Regel 68: Regel 68:
 
</pre>
 
</pre>
  
=== Installatie (jan. 2019 - Python 3.x) ===
+
== Installatie Python 3.5-versie (jan.-okt. 2019) ==
  
 
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.
 
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.
Regel 169: Regel 169:
 
Updaten van Pip middels <code>pip install --user --upgrade pip</code>, werkt tot m'n verbazing óók in één keer.
 
Updaten van Pip middels <code>pip install --user --upgrade pip</code>, werkt tot m'n verbazing óók in één keer.
  
=== Conflict Python 3.5 & 3.7? (okt. 2019) ===
+
=== 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:
+
Ik heb Python 3.7 geïnstalleerd, omdat mysql.connector dat vereist. SQLAlchemy is echter geschreven in Python 3.5. Die twee kunnen dus niet samenwerken.
 +
 
 +
== Installatie Python 3.7-versie (okt. 2019) ==
 +
 
 +
=== Poging 1 ===
  
 
<pre>
 
<pre>
#! /usr/bin/python3
+
pip3 install sqlalchemy
#
+
Requirement already satisfied: sqlalchemy in /usr/local/lib/python3.5/dist-packages (1.2.16)
#####################################################################################
 
# Load libraries
 
#####################################################################################
 
#
 
import sqlalchemy
 
from sqlalchemy import create_engine
 
import mysql.connector
 
 
</pre>
 
</pre>
  
Dit geeft namelijk storingen
+
Helaas: Nog steeds de 3.5-versie.
 +
 
 +
=== Poging 2 ===
 +
 
 +
* Installatiebestanden gedownload naar Desktop & uitgepakt
 +
* Gepoogd te installeren middels <code>python3.7 setup.py install</code>
 +
 
 +
Foutmelding:
 +
 
 +
<pre>
 +
Traceback (most recent call last):
 +
  File "setup.py", line 1, in <module>
 +
    from distutils.command.build_ext import build_ext
 +
ModuleNotFoundError: No module named 'distutils.command'
 +
Error in sys.excepthook:
 +
Traceback (most recent call last):
 +
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
 +
    from apport.fileutils import likely_packaged, get_recent_crashes
 +
  File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
 +
    from apport.report import Report
 +
  File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
 +
    import apport.fileutils
 +
  File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
 +
    from apport.packaging_impl import impl as packaging
 +
  File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 23, in <module>
 +
    import apt
 +
  File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
 +
    import apt_pkg
 +
ModuleNotFoundError: No module named 'apt_pkg'
 +
 
 +
Original exception was:
 +
Traceback (most recent call last):
 +
  File "setup.py", line 1, in <module>
 +
    from distutils.command.build_ext import build_ext
 +
ModuleNotFoundError: No module named 'distutils.command'
 +
</pre>
  
 
== Zie ook ==
 
== Zie ook ==
Regel 194: Regel 226:
 
== Bronnen ==
 
== Bronnen ==
  
 +
* https://www.sqlalchemy.org
 +
* https://www.sqlalchemy.org/download.html
 +
* https://docs.sqlalchemy.org/en/13/intro.html#installation
 
* https://pypi.org/project/SQLAlchemy/
 
* https://pypi.org/project/SQLAlchemy/
 
* https://pypi.org/search/?c=Programming+Language+%3A%3A+Python+%3A%3A+3.7
 
* https://pypi.org/search/?c=Programming+Language+%3A%3A+Python+%3A%3A+3.7

Huidige versie van 9 okt 2019 om 14:13

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

Installatie Python 2.7-versie (okt. 2018 - jan. 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 Python 3.5-versie (jan.-okt. 2019)

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. SQLAlchemy is echter geschreven in Python 3.5. Die twee kunnen dus niet samenwerken.

Installatie Python 3.7-versie (okt. 2019)

Poging 1

pip3 install sqlalchemy
Requirement already satisfied: sqlalchemy in /usr/local/lib/python3.5/dist-packages (1.2.16)

Helaas: Nog steeds de 3.5-versie.

Poging 2

  • Installatiebestanden gedownload naar Desktop & uitgepakt
  • Gepoogd te installeren middels python3.7 setup.py install

Foutmelding:

Traceback (most recent call last):
  File "setup.py", line 1, in <module>
    from distutils.command.build_ext import build_ext
ModuleNotFoundError: No module named 'distutils.command'
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
    from apport.fileutils import likely_packaged, get_recent_crashes
  File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
    from apport.report import Report
  File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
    import apport.fileutils
  File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
    from apport.packaging_impl import impl as packaging
  File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 23, in <module>
    import apt
  File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'

Original exception was:
Traceback (most recent call last):
  File "setup.py", line 1, in <module>
    from distutils.command.build_ext import build_ext
ModuleNotFoundError: No module named 'distutils.command'

Zie ook

Bronnen