2

Say I wrote module foo.py.

I want the installation process to copy foo.py to prefix/lib/pythonX.Y/site-packages so that it can be imported by other modules, but also to create a symbolic link named foo (not foo.py) in prefix/bin/ that points to foo.py.

How does one tell distutils to do that?

8
  • 2
    Generally, I would have the module provide a main function. Then write a separate script which imports foo and calls main... Commented Dec 8, 2013 at 22:05
  • @mgilson This seems to be a question about how to instruct distutils to set up the symlinks as described, not about how to write the code so that it may run as both a module and a script. Commented Dec 8, 2013 at 22:18
  • @qwrty -- Maybe. But the way that I described is the typical (and most suppored?) way to accomplish this sort of thing with distutils as opposed to forcing those two things to be the same script which might be significantly harder. Commented Dec 8, 2013 at 22:26
  • @mgilson I use the if __name__ == '__main__' trick, which I thought was pretty typical. Commented Dec 8, 2013 at 22:32
  • 1
    if you want to limit yourself to distutils then I see all you need on a single page in the docs (script, py_modules options). You could also consider setuptools' entry_points to generate scripts automatically (e.g. with a correct shebang) Commented Dec 8, 2013 at 22:33

1 Answer 1

3

You can do this if you use setuptools entry_points. Here's an example:

foo.py

def main():
    print "Hello world"

setup.py

from setuptools import setup

setup(
    name="foo",
    version = "0.1",
    py_modules=['foo'],
    entry_points = {
        'console_scripts': ['foo = foo:main']
    }
)

Example usage, once the module has been installed using a tool like pip:

$ foo
Hello world
$ python -c 'import foo; foo.main()'
Hello world
Sign up to request clarification or add additional context in comments.

Comments

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.