简体   繁体   中英

Using setuptools in your package's setup.py — How do you conditionally install a script?

How do you configure your setup.py so that it does not attempt to overwrite a script if it already exists? Or at least finish the rest of the package installation if writing a script fails. I am using setuptools for my setup.py . The relevant part of setup.py is this:

entry_points = {
    'console_scripts' : [
        'green = green:main',  # <-- The one I have problems with
        'green%d = green:main' % sys.version_info[:1],     # green2 or green3
        'green-%d.%d = green:main' % sys.version_info[:2], # green-3.4 etc.
        ],
},

my setup.py has both versioned scripts (the version of python is included in the name of the script) and unversioned scripts. On OS XI use system python for 2.7, which requires superuser permissions to run pip (sudo pip ...), which causes the script to get installed under /usr/local/bin with root ownership. Then when I try installing under python 3.4 installed via homebrew with user permissions, it attempts (and fails) to overwrite the unversioned script in /usr/local/bin . I don't mind that it can't overwrite the script file, I do mind that pip fails the install and leaves the half-installed package in a weird, funky state.

I would like to be able to instruct setup.py to either not attempt to install a script if it was already installed by something else, or at least to ignore the error and continue with the rest of the installation if it doesn't have permissions to overwrite the script.

I am not aware, if standard setup function behaviour support your requirement.

But I am sure, following can be done:

Ask setup only for what is really possible

  1. before you call your setup , check, if the script is already present and you are willing to reinstall it or skip. You are in Python script, so you have full Python power at hand, use it to investigate the situation and make the decision.

  2. When calling setup , construct the value of entry_points in such a way, it does what you need (either install or not install the script).

Anyway, this has the dificulty, you need to do your own analysis, if the script to install is already there, where it is, check, if you shall update it or not etc.

Try asking setup for what you want, if it fails, ask for less

As setup is a funcion, you can put it into try and except , if you know, that you can resolve the problem asking to install without first script, second try may go this way.

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