installation path for sqlite database

368
November 26, 2016, at 10:50 AM

I am trying to access a database using python. The src folder is:

ptbl/
├── dialogue.py
├── elem_H.py
├── elems.db
├── __init__.py
├── __main__.py
├── main.py
├── menubar.ui
└── menu.py

with elem_H.py access elems.db database as:

sqlfile = "elems.db"
conn = sqlite3.connect(sqlfile)

Ofcourse, when I am running it from terminal, inside src dir(ptbl), everything works fine. But, when I am outside the src dir,its giving error:

sqlite3.OperationalError: no such table: Overview

and same, if I install it using autotools.

For elems.db to be worked,I have to run it from a folder where elems.db is present.

How can I make it installed in a path?

Answer 1

Mixing python and SQLite files in one single directory is not a good pratice at all. You should fix it and extract elems.db from your libraries directory.

Moreover, as Lutz Horn said in comments, you should make it configurable and not trust that your database file will be always located in the exact same place.

But anyway, to fix your issue without updating these two points, you have to take care of the elem_H.py location. You know elems.db is next to it, so you can do:

import os.path
sqlfile = os.path.join(os.path.dirname(__file__), "elems.db")
  • __file__ store the relative path to your file from the place where you run command.
  • os.path.dirname remove the filename from the given path.
  • os.path.join will concat the computed path and your filename.
READ ALSO
Python3 add colour to specific outputted words from lists in a sentence

Python3 add colour to specific outputted words from lists in a sentence

My below code is currently checking a text file to see if it can find words in a sentence from my lexicon file, if it does find one it then searches this line to see if it can find a word from a secondary list if both of these conditions are met in a line then this line is printed

273
AttributeError: main instance has no __call__ method

AttributeError: main instance has no __call__ method

I'm new to pythonI'm getting "main instance has no call method" error in the below code

577
Python converting a list of strings to unicode

Python converting a list of strings to unicode

I have a list of strings that originally was a list of unicode elementAnd so, some string contains some character accent in unicode formate

455
Plotting filtered dataframe

Plotting filtered dataframe

I am having troubles to understand the following:

412