NameError: name 'create_engine' is not defined in SQLAlchemy - python

I am attempting to create a python script that connects to an SQLITE database and using SQLAlchemy to help with this.
I am still very early, but am trying to create a connection to a new database but keep getting this "create_engine" is not defined in SQLAlchemy error.
To try to simplify I opened a python terminal to try it... see below:
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlalchemy
>>> engine = create_engine('sqlite:///test.db')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'create_engine' is not defined
>>> print(sqlalchemy.__version__)
1.3.18
>>>
At this point I don't even know where to go looking for the problem.
I did a pip uninstall sqlalchemy then pip install sqlalchemy hoping this might help.

Thanks to Gord Thompson. Changing this line:
engine = create_engine('sqlite:///test.db')
to:
engine = db.create_engine('sqlite:///test.db')
made it work!!

Related

Howto fix "ModuleNotFoundError: No module named 'selenium.webdriver.comma' "?

I'm trying to import selenium my first time into my new Python project. But when I run the program it does not work and shows the following message below.
I checked if python and selenium is installed correctly. I even tried to reinstall it but still, the same message shows up. I might have made a stupid mistake somewhere as I'm just starting off with Python and Selenium. Does anyone have a solution?
My code:
from selenium import webdriver
from selenium.webdriver.comma.keys import Keys
driver = webdriver.Firefox()
driver.get("http://www.python.org")
This is the output:
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>>
RESTART: C:\Users\Henryk\Documents\Selenium sample
project\SeleniumSampleProject.py
Traceback (most recent call last):
File "C:\Users\Henryk\Documents\Selenium sample
project\SeleniumSampleProject.py", line 2, in <module>
from selenium.webdriver.comma.keys import Keys
ModuleNotFoundError: No module named 'selenium.webdriver.comma'
>>>
Try to common:
from selenium.webdriver.common.keys import Keys

How do i upload tinkergraph into python/gremlin?

I am trying to use gremlin in python. I imported the following:
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
from gremlin_python.structure.graph import Graph
from gremlin_python import statics
from gremlin_python.process.graph_traversal import __
from gremlin_python.process.strategies import *
from gremlin_python.process.traversal import *
import asyncio
statics.load_statics(globals())
When i run this:
graph = TinkerGraph.open()
graph.io(graphml()).readGraph('air-routes.graphml')
i get the following error:
NameError: name 'TinkerGraph' is not defined
How do i resolve this?
There is no TinkerGraph in Python. In gremlin-python you only get a reference to a graph remotely on a server and that might be a TinkerGraph or something else. If you want to load data that way, you must issue that command as a script through a Client instance:
client = Client('ws://localhost:45940/gremlin', 'g')
client.submit("graph.io(graphml()).readGraph('air-routes.graphml');[]").all().result()
where "graph" in that script is a Graph instance that already exists on the server (and is likely empty). If you're using Gremlin Server, you might consider doing that loading separately as part of Gremlin Server startup as well and then just using gremlin-python to query that data. That would probably be best in this example as the data would just be present when the server is started.
Note that in 3.4.0, we introduce the io() step which will be part of gremlin-python directly at which point you will be able to directly do:
g.io('air-routes.xml').read()
in native python and it will just work (again, the Graph instance must be defined remotely) though the file must be readable by the server.
Here's my working example in the Python shell for submitting a script, first with the tornado error and then without:
$ env/bin/python
Python 3.4.3 (default, Nov 28 2017, 16:41:13)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from gremlin_python.driver.client import Client
>>> client = Client('ws://localhost:8182/gremlin', 'g')
>>> client.submit("g.V()").all().result()
Traceback (most recent call last):
File "/home/smallette/git/apache/incubator-tinkerpop/gremlin-python/target/python3/gremlin_python/driver/client.py", line 51, in __init__
from gremlin_python.driver.tornado.transport import (
File "/home/smallette/git/apache/incubator-tinkerpop/gremlin-python/target/python3/gremlin_python/driver/tornado/transport.py", line 19, in <module>
from tornado import ioloop, websocket
ImportError: No module named 'tornado'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/smallette/git/apache/incubator-tinkerpop/gremlin-python/target/python3/gremlin_python/driver/driver_remote_connection.py", line 45, in __init__
password=password)
File "/home/smallette/git/apache/incubator-tinkerpop/gremlin-python/target/python3/gremlin_python/driver/client.py", line 54, in __init__
raise Exception("Please install Tornado or pass"
Exception: Please install Tornado or passcustom transport factory
>>> exit
Use exit() or Ctrl-D (i.e. EOF) to exit
>>> exit()
$ env/bin/pip install tornado
Collecting tornado
Collecting backports-abc>=0.4 (from tornado)
Using cached https://files.pythonhosted.org/packages/7d/56/6f3ac1b816d0cd8994e83d0c4e55bc64567532f7dc543378bd87f81cebc7/backports_abc-0.5-py2.py3-none-any.whl
Installing collected packages: backports-abc, tornado
Successfully installed backports-abc-0.5 tornado-5.1.1
smallette#ubuntu:~/git/apache/incubator-tinkerpop/gremlin-python/target/python3$ env/bin/python
Python 3.4.3 (default, Nov 28 2017, 16:41:13)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from gremlin_python import statics
>>> client = Client('ws://localhost:8182/gremlin', 'g')
>>> client.submit("g.V()").all().result()
[v[0]]

Python 2.7 64 bit search path

I am trying to load sqlite 64 bit while running Python 2.7 64 bit. I can do this interactively, but, not from a script.
Interactive:
$ /c/Python27-64/python
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:24:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>>
From this script, one single line, the same as was done from the python shell:
import sqlite3
Run from command line:
$ /c/Python27-64/python test.py
Traceback (most recent call last):
File "test.py", line 1, in <module>
import sqlite3
File "c:\Python27-64\lib\sqlite3\__init__.py", line 24, in <module>
from dbapi2 import *
File "c:\Python27-64\lib\sqlite3\dbapi2.py", line 28, in <module>
from _sqlite3 import *
ImportError: DLL load failed: %1 is not a valid Win32 application.
The script is obviously finding a 32 bit dll. But why? What is the difference between interactive and from the single line script? How is the DLL search being modified?
In case anyone runs into this, the problem was the file _sqlite3.pyd in the directory I was running the script. Can someone explain why Python creates it's own version of the Windows dll? Is this simply wrapped so that Python can make calls into it? Perhaps a ctypes wrapper?

Missing .copy() on zlib decompressobj in Python

According to the Python documentation of the zlib module, decompressobj objects have a copy() function to clone the decompressor's state. This equivalent functionality is also mentioned for the compressing side on StackOverflow here.
However, when I try to use it, it simply doesn't exist:
C:\>C:\Python27\python.exe
Python 2.7 (r27:82525, Jul 4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import zlib
>>> meow = zlib.decompressobj()
>>> purr = meow.copy()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: copy
Is this a bug in Python? I tried searching for this subject, but too many false hits get in the way.
Never mind. It's a known bug that was fixed in newer versions of 2.7. I just needed to check the bug list. For some reason, Google didn't index that bug page.

Undefined symbol "afinfo" when importing python-iptables package "iptc"

I am trying to utilize the python-iptables package to list iptables rules in a web app. When I add the iptc package to my environment, I get the below error. I used yum 'provides' to find where the libxtables.so.4 file comes from and found that the iptables and iptables-devel packages were the appropriate choice in CentOS 6.4 x64. I upgraded those packages but it did not change the error.
Does anyone have any suggestions about how I can resolve this?
pgrace#ny-misc01:~/repos/python-iptables/libxtwrapper$ python
Python 2.6.6 (r266:84292, Sep 11 2012, 08:34:23)
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import iptc
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib64/python2.6/site-packages/iptc/__init__.py", line 10, in
from ip4tc import Table, Chain, Rule, Match, Target, Policy, IPTCError
File "/usr/lib64/python2.6/site-packages/iptc/ip4tc.py", line 11, in
from xtables import (XT_INV_PROTO, NFPROTO_IPV4, XTablesError, xtables,
File "/usr/lib64/python2.6/site-packages/iptc/xtables.py", line 744, in
class xtables(object):
File "/usr/lib64/python2.6/site-packages/iptc/xtables.py", line 757, in xtables
_xtables_afinfo = ct.c_void_p.in_dll(_lib_xtables, "afinfo")
ValueError: /lib64/libxtables.so.4: undefined symbol: afinfo
>>>
See this: https://github.com/ldx/python-iptables/issues/25
This is a known problem, old versions of libxtables declared afinfo as static, thus it is not accessible for python-iptables. There is a possible workaround, though - please keep an eye on the ticket, it will be updated as soon as there has been progress.
Another solution is to update iptables on your machine.
Disclaimer: I am the maintainer of python-iptables.
Update: this should be fixed now.

Categories