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.
Related
I use a Gentoo-based Docker image for CI with multiple versions of Python. Recently, I've started experiencing errors because one tool (coveralls) requires sqlite, which is missing. sqlite is part of the Python standard library.
This can be checked from the command line
>>> removing all .pyc files
>>> executing command
me#5b35f99c08af /source $ python
Python 3.6.9 (default, Dec 27 2019, 12:15:49)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'sqlite3'
I couldn't find any notes in the Gentoo packaging database about this, but I'm not really familiar with it as an OS. I assume there must have been a problem building some relevant library.
Python has been installed like this:
RUN emerge -q -u dev-lang/python:3.6
But the error occurs for all the versions I'm currently testing with: >= 3.5. Any ideas as to what I'm doing wrong?
I'm unable to run nosetests on my tests, because of module import errors.
I am running inside of a virtualenv with global site packages enabled.
I can successfuly run the tests from the IDE (pycharm). I can also import the module directly from cli. I can also (not shown) run a python script tht imports the troublesome module.
I assume this is a path issue, but haven't been able to discover the root cause. I have also toggled no-path-adjustsment in .noserc to no avail.
(ipc)➜ Analytics git:(feature/ipc_via_rest) pip freeze | grep arrow
arrow==0.7.0
(ipc)➜ Analytics git:(feature/ipc_via_rest) python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import arrow
>>> arrow.utcnow()
<Arrow [2015-11-30T16:29:57.363278+00:00]>
>>> exit()
(ipc)➜ Analytics git:(feature/ipc_via_rest) nosetests src/bnr_analytics_data_services/
<elided>
ImportError: No module named arrow
Try running nosetest --pdb. It will bring up pdb debugger on error. Look at sys.path for abnormalities.
I am getting the following errors when trying to run a piece of python code:
import: unable to open X server `' # error/import.c/ImportImageCommand/366.
from: can't read /var/mail/datetime
./mixcloud.py: line 3: syntax error near unexpected token `('
./mixcloud.py: line 3: `now = datetime.now()'
The code:
import requests
from datetime import datetime,date,timedelta
now = datetime.now()
I really lack to see a problem. Is this something that my server is just having a problem with and not the code itself?
those are errors from your command shell. you are running code through the shell, not python.
try from a python interpreter ;)
$ python
Python 2.7.5+ (default, Sep 19 2013, 13:48:49)
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> from datetime import datetime,date,timedelta
>>>
>>> now = datetime.now()
>>>
if you are using a script, you may invoke directly with python:
$ python mixcloud.py
otherwise, ensure it starts with the proper shebang line:
#!/usr/bin/env python
... and then you can invoke it by name alone (assuming it is marked as executable):
$ ./mixcloud.py
Check whether your #! line is in the first line of your python file. I got this error because I put this line into the second line of the file.
you can add the following line in the top of your python script
#!/usr/bin/env python3
I got this error when I tried to run my python script on docker with docker run.
Make sure in this case that you set the entry point is set correctly:
--entrypoint /usr/bin/python
When running this command in my virtual environment:
sudo ./manage.py migrate wifipromo 0088_auto__add_adgrouptimesets
Traceback (most recent call last):
File "./manage.py", line 2, in <module>
from django.core.management import execute_manager
ImportError: No module named django.core.management
I get the error above. I checked the shebang in manage.py its "#!/usr/bin/env python" which should be correct.
Additionally when I enter the python console, I can run the following....
(ve)ubuntu#app02:/var/apps/nimbus/nimbus$ python
Python 2.6.5 (r265:79063, Feb 27 2014, 19:43:51)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django
<module 'django' from '/var/apps/nimbus/environments/767a6ca970ca5b5cc5724c3e158f8f53/ve/lib/python2.6/site-packages/django/__init__.pyc'>
>>> django.get_version()
'1.4'
which is strange. Anybody know what's wrong?
You're running with sudo (user root), so it can not get info from your virtualenv (user ubuntu). Try to run migrate as normal user.
Turns out the app I sshed into was configured for a specific user. Ubuntu and root aren't configured correctly. This turned out to be a very specific domain problem and I guess its not a good format for this site. Sorry. Thanks for helping everyone.
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