[Tutor] What is the easiest way to ensure the current working directory is the same as the directory where the main program is saved?

Cameron Simpson cs at cskk.id.au
Thu Jul 1 21:37:08 EDT 2021


On 01Jul2021 19:02, boB Stepp <robertvstepp at gmail.com> wrote:
>Cameron brought up virtual environments for segregating one's programs
>and their dependencies during development. [...]
>It is hard enough keeping
>up with the pace of Python updates and having to reinstall the
>packages I use for my personal use!

I usually don't update Python whenever a release happens. Do it at need 
(new feature or bugfix or security fix). Or, of course, when a brew or 
OS update yanks the old Python out from under me and force a venv 
rebuild.

One nice thing about a venv (well any pip-based install really) is that 
you can stick your required module names in a file (with optional 
version requirements) and just load them back up if you have a fresh 
venv. Conventionally that file is called 'requirenets.txt", but you can 
use any name (I use venv-requirements.txt in my personal dev env). Then 
for a fresh install you go:

    ...../bin/python -m pip install -r requirements.txt

and it does all the hard work for you.

>Question:  I am about to embark on writing a relatively small program
>that I will use to further explore the use of SQLite for storing data,
>something I keep dabbling with off and on.  As you know SQLite stores
>each database as a single file.  In this thread's data location
>discussion, where should this database file be stored?

IMO, because it is a file, wherever you would store any other kind of 
user app data.

I have a few SQLite dbs myself. I keep a personal directory named "var" 
in my home directory and typically choose:

    ~/var/appname.sqlite

or similar. On Windows, I gather you've got a conventional 
per-user-app-data area, which I'd use instead.

Your objective is (a) keep the data suitable partitioned from other 
data, so probably per-user per-app and (b) a fairly simple easy to 
remember naming convention.

Finally, remember Heuer's Razor:

    If it can't be turned off, it's not a feature. - Karl Heuer

Let the user provide an environmnet variable or command line switch to 
specify the data location. If your app has several state files, eg an 
sqlite db and some other things, that might be a folder name where 
several things live.

>And for more
>substantial database programs than SQLite, how would that normally be
>handled if this database program is integrated into the overall
>application structure?

To take an example of PostgreSQL, you would usually have a shared 
database server (local to your machine or not), and individual databases 
for various apps.  For "personal" app data I'd typically involve both 
the user's login name and the app name in the "database name", eg 
"appname_cameron" perhaps.  Or "cameron_appname". Just to partition off 
the user's app data from other users' app data.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list