1

I have a python package targeted at linux machines that needs to install its locale files to an accessible location. Right now, I have them being installed to sys.prefix + "/share/locale/".

However, I found a small caveat with Ubuntu and pip. Under default conditions, Ubuntu installs packages installed with pip to /usr/local and sets sys.prefix to that during installation. However, after installation, when the package is run, the prefix is /usr, meaning my code can't find the locale files installed at /usr/local.

I could simply hardcode the location, but I would prefer not to do this, as it makes the package less portable and would require the user to install it as root. These are added as data_files in setup.py and won't be discoverable as a python package.

How else can I ensure my package can find my the locale files after installation?

I thought about adding a line to the package's __init__.py during installation, which created a variable pointing to the locale dir's location. However, it did not seem trivial to edit files being installed without changing the source files.

This is a python 3 only package.

2
  • Are you sure you don't have two Pythons — in /usr/local/bin and in /usr/bin? I don't believe Python changes sys.prefix that way. But 2 different Pythons may have 2 different prefixes. Commented Jul 10, 2017 at 14:14
  • @phd find `echo $PATH | tr ':' " " -name python3` only shows /usr/bin/python3. I'm guessing this happens because Ubuntu wants the files installed by the user with pip to go to /usr/local so it changes the prefix, but it then needs to change it back for the main machine, Commented Jul 10, 2017 at 14:38

1 Answer 1

1

Maybe use the resource functions available in pkg_resources to find the files?

from pkg_resources import resource_stream, resource_filename
with resource_stream('my_package', 'locale/foo.dat') as infp:
    # ...
# ... or ...
foo_location = resource_filename('my_package', 'locale/foo.dat')
Sign up to request clarification or add additional context in comments.

5 Comments

So how would this help me if I don't know the path to the files? These are data files, they won't be in a package.
Point is they should be in your package hierarchy if they're data files that your package requires.
Really? Locale files are supposed to go under /usr/share/locale (at least under debian). I don't think it would be appropriate to make that a package. I also am compiling .po to .mo files with gettext, so adding it as package data is a little annoying, but I guess possible.
Okay, I thought we were talking about generic Python packages, not Debian packages :) I have to admit I don't really know much about Debian packaging.
It is a generic python package, which I am also evantually packaging in Debian. The point is, setup.py should be able to install to a different directory. I'm guessing I'll have to add it as package data and just keep it in the module directory. Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.