2

Sorry if this question is unclear.

I wrote a library in python, to be uploaded to PyPI (pip). I'd like to have my program run and respond to inputs on an environment variable.

For example, refer to this library: https://github.com/rg3/youtube-dl

After installing it via pip, users can instantly call upon the program via.

$ pip install youtube-dl
$ youtube-dl http://youtube.com/video?v=sdfafd7f6s

# What's cool is that the above even works in a virtualenv!

I'd also love for my program to be put on an environment variable, but i'm not sure how to set this up.

Any clues? Thanks!

Lucas Ou-Yang
  • 5,505
  • 13
  • 43
  • 62
  • 2
    er.. I don't think this has anything to do with environment variables? Executables on your PATH can be run without specifying the absolute path. – roippi Feb 22 '14 at 06:15
  • You are right, I think the example I provided modifies the path but i'd like to know the best way to do so during the installation process. – Lucas Ou-Yang Feb 22 '14 at 06:18
  • 1
    A better title for your question might be, "How to install a Python program as an executable?" – cbare Feb 22 '14 at 06:37

1 Answers1

2

Your question is unclear. It looks like what you want is to run your program from the command line without explicitly invoking the Python interpreter.

To do this, you just need a few lines in your setup.py to declare an entry point. That is explained in Automatic Script Creation. Basically, you need a console_scripts item:

setup(
    # other arguments here...
    entry_points = {
        'console_scripts': ['foo = my_package.some_module:main_func']
    }
)

You can see something similar in lines 68-71 of the setup.py file for youtube-dl.

If you really want to read environment variables, use environ from the os module.

import os

try:
    important_info = os.environ['IMPORTANT_INFO']
except KeyError:
    raise Exception('Set IMPORTANT_INFO environment variable, please!')
Community
  • 1
  • 1
cbare
  • 12,060
  • 8
  • 56
  • 63