I'm trying to use sqlite with python via the import function, but it looks that python can't find sqlite.
My sys.path contains the following:
['', '/usr/local/share/python', '/usr/local/Cellar/python/2.7.3/lib/python2.7/site-packages/distribute-0.6.26-py2.7.egg', '/usr/local/Cellar/python/2.7.3/lib/python2.7/site-packages/pip-1.1-py2.7.egg', '/usr/local/Cellar/python/2.7.3/lib/python27.zip', '/usr/local/Cellar/python/2.7.3/lib/python2.7', '/usr/local/Cellar/python/2.7.3/lib/python2.7/plat-darwin', '/usr/local/Cellar/python/2.7.3/lib/python2.7/plat-mac', '/usr/local/Cellar/python/2.7.3/lib/python2.7/plat-mac/lib-scriptpackages', '/usr/local/Cellar/python/2.7.3/lib/python2.7/lib-tk', '/usr/local/Cellar/python/2.7.3/lib/python2.7/lib-old', '/usr/local/Cellar/python/2.7.3/lib/python2.7/lib-dynload', '/usr/local/Cellar/python/2.7.3/lib/python2.7/site-packages', '/usr/local/Cellar/python/2.7.3/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info', '/usr/local/Cellar/python/2.7.3/lib/python2.7/site-packages/IPython/extensions']
But sqlite is listed in usr/lib.
Edit:
As suggested, I tried import sqlite3, but python returns this error:
dlopen(/usr/local/Cellar/python/2.7.3/lib/python2.7/lib-dynload/_sqlite3.so, 2): Library not loaded: /usr/local/lib/libsqlite3.0.8.6.dylib
Referenced from: /usr/local/Cellar/python/2.7.3/lib/python2.7/lib-dynload/_sqlite3.so
Reason: image not found
How do I load sqlite?
There is no need to add anything to your module search path; the SQLite module comes with the Python standard library. However, you misspelled the name of the module, it is called sqlite3 (note the 3 on the end):
Python 2.7.3 (default, Oct 22 2012, 06:12:32)
[GCC 4.2.1 Compatible Apple Clang 3.1 (tags/Apple/clang-318.0.58)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>> sqlite3.connect(':memory:')
<sqlite3.Connection object at 0x105354200>
If you get errors still, your homebrew installation is broken; you probably ran into this bug. Run:
brew rm sqlite python
brew install python
to repair.
sqlite3 is the name of the python module which provides an interface between Python and the sqlite database engine. sqlite is the name of the underlying database engine.
So (as Martijn Pieters already pointed out) try:
import sqlite3
Related
I cannot find the test module in my Anaconda's version of Python. Can anyone help me fix this. This module is used by the dpkt library that I am trying to use.
Python 2.7.8 |Anaconda 2.1.0 (x86_64)| (default, Aug 21 2014, 15:21:46)
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
>>> import test
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named test
Quick Fix: You can checkout a copy of pystone.py from the cpython github repository and copy it to a test directory that is present in your PYTHONPATH. Or you would copy it to a test directory in your python project's root (ugly way).
Perhaps Anaconda Python does not ship with a copy of the test module. This is a standard part of Python 2.7. Other times, users accidentally overwrite their Python standard library's test module with something else. You can try to use the Python version that is shipped with OS X instead. If that fails as well, then try to see which test module is being loaded, and go from there.
import test
print test
Alpine Linux ships a python2-tests package.
the quickest way to fetch it if you don't have an Alpine lxc container is from a main repo here (or apk fetch python2-tests inside lxc).
the .apk can be uncompressed with an archiver to a .tar.gz & then just uncompress again.
I am trying to resolve some dependencies for the MYSQL connector for Python on Angstrom.
From the command line I get the following error:
Python 2.6.6 (r266:84292, Feb 25 2011, 16:50:01)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import io
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named io
I thought that IO was a base-level module. It appears to be up-to-date:
# opkg install python-core
Package python-core (2.6.6-ml12.2.6) installed in root is up to date.
Shouldn't IO be available by default, and what can I do to resolve this issue?
Thanks to Padraic Cunningham who lead me on the path to a solution.
I found that several files, including io.py were missing from the install. Building the Angstrom for the Beagleboard (XM) image from the Angstrom website did not include these files (or subsequent modifications I made deleted them?). I rebuilt Python, keeping the identical version, from the Python source.
I had to then rebuild the MySQL connector (from MySql/Oracle). (All my other modules including OpenCV continued to work without issue)
This worked and I am now able to query the database.
One additional note. Once all the dependencies were resolved, I still could not connect to the database. The problem was that the MySQL connector was assuming a TCP/IP connection rather than file based socket. So I had to add the following to the connection string:
unix_socket="/tmp/mysql.sock"
Such that the full connection string looked like this:
cnx = mysql.connector.connect(user='username', password='password', host='localhost', database='dbname', raise_on_warnings=True, unix_socket="/tmp/mysql.sock")
I just got python and typing:
sqlite test.db
into the shell, but I get a syntax error. What have I missed?
I guess that you did the following?
$ python
Python 2.7.3 (default, Sep 26 2012, 21:51:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> sqlite test.db
File "<stdin>", line 1
sqlite test.db
^
SyntaxError: invalid syntax
Try this instead:
import sqlite3
conn = sqlite3.connect('test.db')
cursor = conn.cursor()
cursor.execute('''Your query goes here''')
For more details, take a look at the sqlite documentation for python2 or python3
Python doesn't provide this command-line utility so make sure sqlite3 is in your path. Then you can either execute:
$ sqlite3 mydb.db
or if you have entered your settings in settings.py:
./manage.py dbshell
I think you want to use the sqlite3 command line tool to create a new database. For this you should use your system terminal not the python console. So the command should look like so (on a linux system):
$ sqlite3 test.db
Verify if sqlite exist in PATH and what are privileges for file test.db.
I just installed lxml for parsing xml file in python. I am using TextMate as an IDE. Problem is that when I try to import lxml (from lxml import entree) then I get
ImportError:'No module named lxml'
But when I use Terminal then everything is fine
Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from lxml import etree
>>> root=etree.element("root")
>>> root=etree.Element("root")
>>> print (root.tag)
root
>>> root.append(etree.Element("child1"))
>>> child2 = etree.SubElement(root, "child2")
>>> child3 = etree.SubElement(root, "child3")
>>> print (etree.tostring(root,pretty_print=True))
<root>
<child1/>
<child2/>
<child3/>
</root>
It's pretty weird. Does it have something to do with TextMate?
Suggestion Please!
This most probably means that you have more than one python installation on your system and that TextMate and the Terminal using different ones by default.
One workaround: In your python file, you can specify an interpreter directive to point to the python installation (and executable) of your choice:
#!/usr/local/bin/python
# Even thought standard python is in /usr/bin/python, here we want another ...
You need to define the shell variables in TextMate's settings, specifically 'TM_PYTHON' needs to point to your Python binary.
To find which Python your using, in a terminal you could type 'which python'
It's likely that TextMate is using a different PYTHONPATH than your terminal. I'm not a TextMate user so I can't help you there, but it should point you in the right direction.
You might be be running a different version of Python from TextMate. I had a similar issue with RedHat having 2 versions of Python. I had installed the module to one, but was trying to execute with another.
Does anyone know where i can find this python module 'contextlib'?
root#overo:~# python
Python 2.6.6 (r266:84292, Mar 9 2011, 10:05:36)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import contextlib
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named contextlib
I did not compile python myself personally. I'm just in this mess. It's running on an ARM based CPU so maybe some things were left out to save space.
I ran find / | grep contextlib which resulted in nothing.
Can i download this module from somewhere and just plonk it in /usr/lib/python2.6? Will that work?
I got this error in a different way.
I created a pipenv virtual environment using the 32bit version of Python 3.6.5 on Windows 10. I then realized I needed the 64bit version. Uninstalled the 32bit, installed the 64bit, and then tried to go back to my existing virtual env. The previously created env was now broken in odd ways and gave me this error.
I solved this by removing the old pipenv pipenv --rm and creating a new one with the newly installed version of python.
As others have noted, that module should be in the standard library, but if it's an embedded device, it may have been dropped to save space (if true, a foolish choice IMO, since leaving out contextlib.contextmanager robs the with statement of much of its power and convenience)
If you can name the specific device or manufacturer (or ask the vendor directly), you may be able to get a better answer.
As far as fixing it goes, grabbing http://hg.python.org/cpython/file/2.6/Lib/contextlib.py and dropping it in sys.path somewhere should do the trick (running python -m site will dump the list of directories that you can use)
It has been part of the standard library since 2.5 according to the docs. It seems a bit weird that you don't have it, it works with 2.6.6 for me (Ubuntu 10.10):
blair#blair-eeepc:~$ python
Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import contextlib
>>> contextlib.__file__
'/usr/lib/python2.6/contextlib.pyc'
Somebody may have a better suggestion, but if it comes to it there is a link at the top of the documentation to the source code (which is Python, so you should be able to use it directly without any compilation or anything).
Edit: Unless, as Santiago Lezica suggested, you compiled your copy of Python manually, in which case it should be a simple matter of copying the module into the correct library path.
Edit for updated question: To the best of my knowledge, just dropping the source into a directory on the Python path should work. You could do this in the system library, but, to avoid it being deleted/replaced/otherwise borked in future updates, I'd recommend putting it in a separate directory and adding that directory to the Python path. You could put it under /usr/local, or somewhere in your home directory.
With Angsrom Linux, contextlib is included in the python-misc package. You can grab it by running:
opkg install python-misc
This won't, however, get you all expected python modules, so you may also want to install python-modules:
opkg install python-modules
I found one more occasion, which produces the same error.
I had made a virtual environment with python 3.6. After a updated my python version to 3.7 I tried to activate the old virtual environment and got this error.
The solution was to delete the old environment and recreate it with the new python version.
Check sys.path to make sure your python interpreter is looking in the right directories. It should look something like this (not necessarily identical):
>>> import sys
>>> sys.path
['', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/local/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages/PIL', '/usr/lib/pymodules/python2.6']
EDIT: With the updated information in the question that this is an install of unknown origin on a constrained device, assuming that unnecessary modules were removed to save space makes sense. However, for the record, I'll mention another, perhaps more common scenario where modules cannot be found: when there are file permissions issues. For example:
$ python -c 'import contextlib; print(contextlib.__file__)'
/usr/lib/python2.6/contextlib.pyc
$ ls -l /usr/lib/python2.6/contextlib.py*
-rw-r--r-- 1 root root 4136 Dec 26 16:42 /usr/lib/python2.6/contextlib.py
-rw-r--r-- 1 root root 4127 Jan 1 21:45 /usr/lib/python2.6/contextlib.pyc
$ sudo chmod go-r /usr/lib/python2.6/contextlib.py*
$ python -c 'import contextlib; print(contextlib.__file__)'
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named contextlib
Especially with custom installations, import problems due to file permission issues and path problems are some of the easiest things to check and, usually, to fix.
Python 2
sudo apt-get install python-contextlib2
Python 3
sudo apt-get install python3-contextlib2
contextlib was introduced in Python 2.5, can you remove and re-install your Python 2.6.6 again? From my copy of Python 2.6.6:
Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import contextlib
>>>