takluyver / pynsist

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

Make documentation clearer regarding data files and nsis constants #38

Closed aaren closed 9 years ago

aaren commented 9 years ago

The documentation mentions nsis constants and how data files can be redirected to them using > in the config file. However, it isn't made clear how to access those nsis constants from within the python program.

Is the default destination nsis $INSTDIR?

If I have this config:

[Include]
files = resources/file

Then what should my python look like to access resources/file?

The following code works if I execute the application from the install dir, but fails if I use the start menu shortcut:

fname = 'resources/file`
with open(fname) as fp:
    print(fp.read())

What do I have to do to fname to have the application find it?

takluyver commented 9 years ago

Relative paths (like 'resources/file') are relative to the working directory. When you run the application from the shortcuts, the working directory is the user's home directory (typically C:\Users\{username}).

The easiest thing is to put data files inside your Python packages - then they are copied as part of the package (no need to list the files separately in the config file), and you can access them in your code like this:

data_file_path = os.path.join(os.path.dirname(__file__), 'file.dat')

If you prefer to copy them separately, then you can get the path of the script that started your app (which should be in the installation directory) using sys.modules['__main__'].__file__.

Yes, the default destination is $INSTDIR.

aaren commented 9 years ago

Thanks :), that's what I was after: sys.modules['__main__'].__file__

I can check that works in a couple of days.

Is there a way to access the nsis constants at runtime (e.g. through os.environ)?

takluyver commented 9 years ago

Runtime doesn't know about nsis at all (and this is deliberate - I don't want to tie application code to the install mechanism). You can get some things via Windows environment variables, but the official way to look up the locations is the Windows 'Known Folders' API (I think - I'm not really a Windows programmer). It looks like there's a winpath module that wraps that API using ctypes, or pywin32 has an interface for it.

Out of interest, what application are you looking to package?

aaren commented 9 years ago

That makes sense. I'm not a Windows programmer either :) but stuff like pynsist makes it a lot more bearable. I'm migrating from pyinstaller.

The application is proprietary. Can't say much but it helps engineers model and make choices about things.

takluyver commented 9 years ago

Thanks! I'll have a think about what I can add to the docs to make this clearer.

aaren commented 9 years ago

If you don't get round to it I'll make a PR when I'm working on this again in the next couple of days.