Packaging Python script with UTF-8 mode

Hi there!

Suppose I have a Python script that I want to distribute (not necessarily on PyPI, but I want people to be able to use pip to install it, e.g., with pip install . in a source checkout, or with pip install

Furthermore, I am excited about Python’s UTF-8 mode, finally allowing me to (mostly) not care about encodings anymore, because I adhere to the principle that the world should use UTF-8 ALWAYS, UTF-8 EVERYWHERE, UTF-8 ONLY.

Is there any way to ensure that when pip creates the wrapper for my script, it passes -X utf8 to the Python interpreter?

I didn’t find anything about this on pyproject.toml resources or on Entry points specification — Python Packaging User Guide.


1 Like

I don’t have an answer, but I do think it’s a good idea.

I also think there has got to be a way to set UTF-8 mode in your code, after Python startup, but can’t find that either :frowning:

On the other hand, you can also add the encoding='utf-8' argument to all the open(), etc calls in your code, and that will solve most of the problems for your users.

Yeah. But it’s easy to forget one (including when it’s buried deep into a dependency), so I’d rather use UTF-8 mode :frowning:

One more note: pip itself does not setup the entry points – that’s done by the “build” tool – e.g. setuptools. So you might look there – and maybe post an issue here:

Maybe it is a question for the #packaging category.

If I am not mistaken, it is indeed the installer (pip for example) who is in charge of writing the script wrapper. And I believe that in pip’s case, it is done by distlib.scripts which is vendored in pip.

As far as I know pip does not offer any way to customize the shebang that it generates.


Thanks – looksl ike you are right (kinda – if you do the old fashioned install then setuptools does it – I guess that pip is doing it with wheels.


“You can pass an optional options dictionary to the make() method. This is meant to contain options which control script generation.”

" interpreter_args : If provided, this should be a list of strings which are added to the shebang line following the interpreter. If there are values with spaces, you will need to surround them with double quotes."

So – how to tell pip what you want it to do?? But it may not be pip directly, it may be how the wheel is built … so ??

Yes, you are more likely to find help in the Packaging topic – or on the pip (or setuptools or poetry or ??) gitHub issues.

Thanks for the replies! I’ll raise this in #packaging.