takluyver / pynsist

Build Windows installers for Python applications
https://pynsist.readthedocs.io/
Other
882 stars 119 forks source link

working directory vs. install directory #159

Closed sprocket1 closed 5 years ago

sprocket1 commented 5 years ago

From the documentation: "Working directory If users start your application from the start menu shortcuts, the working directory will be set to their home directory (%HOMEDRIVE%%HOMEPATH%). If they double-click on the scripts in the installation directory, the working directory will be the installation directory. Your application shouldn’t rely on having a particular working directory; if it does, use os.chdir() to set it first."

In order to get around this, I changed the pyapp.nsi template to start in $INSTDIR instead, and that seems to work fine for my purpose since my app creates and appends files that are in the same directory as the app.launch.py is (the install directory). With this mod, the files are created correctly no matter whether launched from the shortcut or launched by double-clink the script.

However, I'm curious as to how one would modify their application (using os functions, etc.) so as to always create files relative to where it is installed? How could it know where the user chose to install it? Would this require a registry lookup?

takluyver commented 5 years ago

It's generally a bad idea to create files in the installation directory: if the user installs the application systemwide, it won't have write access to its own install directory when it runs. There are standard locations like APPDATA for storing files the application creates but doesn't want the user to look at.

But if you want to work out where the install directory is, that's fairly straightforward as well. See the FAQ: http://pynsist.readthedocs.io/en/latest/faq.html#using-data-files

sprocket1 commented 5 years ago

Thanks for that insight. I took your advice and changed things around to store my program-created files (which the user needs to see) in the ProgramData folder, which is what that folder seems to be intended for. I used this code: ProgramData = os.getenv('programdata') MyDir = os.path.join(ProgramData, 'MyApp') if os.path.isdir(MyDir) == False: try:
os.mkdir(MyDir) except Exception as e: print(type(e)) print(e)

Now everything seems good, and it works properly whether the user chooses only for user or for any user.

I think we can close this issue.

takluyver commented 5 years ago

OK, great.