简体   繁体   中英

Python 2.3 distutils overwrites shebang with /usr/bin/python instead of /usr/bin/env python

I have a package that I install on a shared dev system. I would like the scripts that the package brings in to be available just for my user (or anyone else who asks for it). To install the package, I am using the following command from the package directory:

python setup.py install --home=~

This does everything that I would like it to do thus far. It installs my modules and puts the scripts in the /home/$(whoami)/bin directory.

One thing that it does, however, is replace the shebang that I have in the scripts file. So, instead of the nice friendly #!/usr/bin/env python , it replaces it with #!/usr/bin/python

Now, since my package is only being installed for the current user, it cannot see modules within the package for import.

So, since my scripts import functions from the modules within the package, running them from the command line results in an import error.

Running an interactive python interpreter allows me to import the files just fine using the exact same text as the import line from the script I want to run.

Does anyone know of a way that I can address this problem?

PS No, I have no control over the version of Python in this setting. It's 2.3.4 for the foreseeable future.

Edit: I forgot to mention the --executable option. I have no idea if that was present in 2.3 or not, but it works in 2.7 to override the rewriting of hashbangs with whatever the current interpreter is. Try that before anything else.

In the event the --executable switch doesn't work you should be able to write your own setup.py the way you want. The critical thing about setup.py is that it has to call distutils.core.setup(). If distutils is doing something silly when setup() is called, add a correction to your setup.py's routine after that:

from distutils.core import setup
import os
setup(name='foo', version='1.0', py_modules=['spam', 'eggs'])
fix_hashbang(script_list):
    sed = "sed -i 's/#! \/usr\/bin\/python/#! \/usr\/bin\/env python/'"
    cmd = ' '.join([sed, ' '.join(script_list)])
    os.system(cmd)
fix_hashbang(list_of_scripts)

Where "list of scripts" would be a list of strings containing the paths of the scripts to be updated. This is a bit of a wonky solution. I know the --executable thing works in 2.7, and surely there must be something comparable in 2.3 even if the same switch doesn't exist exactly like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM