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

boB Stepp robertvstepp at gmail.com
Sat Jun 26 15:14:23 EDT 2021


Please forgive the top posting in this instance.  I want to ensure
Dennis' response gets saved intact into the Tutor archives so I can
easily find it later as his postings otherwise never show up unless
someone responds to them.

In summary of what you write below, Dennis, you are basically saying
to me to store my data files in standard locations expected by each
operating system that the program is installed into?  Then this should
be a no-brainer for the actual program to access its data and
configuration files.

But what about during a program's development phase?  It is handy to
have data files in the same project directory space as everything
else.  I have been doing some online searching and it appears that
during an installation process that installers can copy data and
configuration files to those standard locations that you are
advocating.  But again I know nothing about installing Python programs
and the software that gets it done.  I merely know how to use pip to
install applications others have developed, not how to get my code
ready for such a procedure.

Anyway, thanks Dennis for your detailed response!

boB Stepp

On Sat, Jun 26, 2021 at 12:10 PM Dennis Lee Bieber
<wlfraed at ix.netcom.com> wrote:
>
> On Fri, 25 Jun 2021 22:50:52 -0500, boB Stepp <robertvstepp at gmail.com>
> declaimed the following:
>
>
> ><Scratch head>  But I *do* care about where my source code winds up,
> >don't I?  How else do I load data from its data folder if I don't know
> >where I am in the user's file system?
>
>         Most applications do not store live "data" in their installation
> directory. On Windows, that is what each user's AppData directory is used
> for (though many applications coming from Linux/UNIX background create
> "dot" files in %userprofile% (in Linux, "dot" files are normally hidden)
>
> C:\Users\Wulfraed>echo %userprofile%
> C:\Users\Wulfraed
>
> C:\Users\Wulfraed>dir %userprofile%
>  Volume in drive C is OS
>  Volume Serial Number is 4ACC-3CB4
>
>  Directory of C:\Users\Wulfraed
>
> 06/22/2021  03:05 PM    <DIR>          .
> 06/22/2021  03:05 PM    <DIR>          ..
> 12/05/2020  04:57 PM             6,514 .bash_history
> 06/24/2021  06:28 PM               866 .flexprop.config
> 07/29/2019  01:48 PM               166 .gitconfig
> 09/13/2020  02:39 PM             2,277 .kdiff3rc
> 05/31/2021  07:11 PM             1,367 .python_history
> 05/07/2021  04:25 PM             2,596 .RData
> 05/07/2021  04:25 PM               166 .Rhistory
>
>         If the program requires default configuration to exist, it uses,
> perhaps, __file__ to find the install directory, uses various os.path
> methods to parse off the filename itself leaving the directory, and then
> combines the directory with the known subpath/config-data -- and copies
> that to the user specific location
>
> C:\Users\Wulfraed>echo %appdata%
> C:\Users\Wulfraed\AppData\Roaming
>
> C:\Users\Wulfraed>dir %appdata%
>  Volume in drive C is OS
>  Volume Serial Number is 4ACC-3CB4
>
>  Directory of C:\Users\Wulfraed\AppData\Roaming
>
> 06/02/2021  02:51 PM    <DIR>          .
> 06/02/2021  02:51 PM    <DIR>          ..
> 12/03/2016  06:28 PM    <DIR>          .kde
> 12/03/2016  06:28 PM    <DIR>          .mplab_ide
> 12/03/2016  06:28 PM    <DIR>          .oit
> 01/03/2017  01:54 AM    <DIR>          5KPlayer
> 12/03/2016  06:28 PM    <DIR>          Acronis
> 10/10/2017  12:28 PM    <DIR>          Adobe
>
> >
> >Maybe this is where my misunderstandings are occurring.  To date, I
> >have had no installation process.  Any programs that I need to be
> >elsewhere, I copy the program to its new living facilities, whether
> >elsewhere on my PC, somewhere else on a network, or to another PC.
> >Does an actual installation process make these issues go away?  As a
> >user of other people's programs that come with an installer one of the
> >first choices the user usually must make is accept the default
> >installation suggestion or choose a different location.  I suppose
> >this is where the needed information is fed into the program and
> >stored for later use?
>
>         The install location might be an entry in the Windows registry
> (especially if the application has an uninstall feature). About the only
> things done in an install directory (for Python) is to compile .py files
> (modules) into .pyc; that often needs to use the local Python, and reduces
> the distribution file size.
>
>         An installer program may handle copying the default config files to the
> user's directory, where the application expects to find them.
>
> >
> >I thought I could put off figuring out how to install Python packages
> >the "proper" way (if there is one) ...
> >
>
>         Are you talking packages you are writing? If so, the main feature is to
> have them in a directory that Python searches during import.
>
> https://docs.python.org/3/tutorial/modules.html
> """
> 6.1.2. The Module Search Path
>
> When a module named spam is imported, the interpreter first searches for a
> built-in module with that name. If not found, it then searches for a file
> named spam.py in a list of directories given by the variable sys.path.
> sys.path is initialized from these locations:
>
>     *   The directory containing the input script (or the current directory
> when no file is specified).
>
>     *   PYTHONPATH (a list of directory names, with the same syntax as the
> shell variable PATH).
>
>     * The installation-dependent default.
> """
>
> >------------------------
> >import os
> >
> >print("Testing!")
> >print("Current working directory: ", os.getcwd())
> >print("Source code file: ", __file__)
> >with open('test_text.txt') as f:
> >    print(f.read())
> >------------------------
> >
>
> with open(os.path.join(os.path.dirname(__file__), "test_text.txt")) as f:
>
> >
> >How should I be handling these situations?
> >
>
>         Personally... by not putting /data/ in the application space. Arrange
> for it to be in user-specific space...
>
>         Windows: %userprofile%/.myapp/config or %appdata%/myapp/config
>         Linux:  ~/.myapp/config
>
>         Also consider the first bullet in the module search path quote... If
> you arrange for your "data" to be a .py file, you should be able to just
>
> import config
>
> (may need to ensure you don't have conflicting names)
>
>
> --
>         Wulfraed                 Dennis Lee Bieber         AF6VN
>         wlfraed at ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list