What scripts would go into a bin folder of a Python package?

前端 未结 2 1461
忘掉有多难
忘掉有多难 2021-02-18 15:09

I\'m learning about Python Packages from Learn Python the Hard Way and one of the exercises it says:

Put a script in the bin directory that you can run

2条回答
  •  醉话见心
    2021-02-18 15:45

    I just recently got through Ex46 in LPTHW myself. Like you, I was confused by the scripts. In case the other answer was too advanced for you, I ended up just putting in a simple "hello world" script:

    #!/usr/bin/env python
    
    from test3 import printstring
    printstring.printstring("test script working")
    print "test over"
    

    I named that file testscript3.py (*Note, I learned later that it would be more convenient to leave off the .py filename extension if it were a real script that I wanted to seem like a system command)

    My file test3.py was like so:

    def printstring(s='you did not provide string'):
        print s
    

    Here's some newbie things that I learned while trying to get this process to work:

    • The #! symbol is sometimes pronounced shebang and the simple explanation is that the command on that line tells the shell to use python to run the script. If you leave off the ".py" filename extension, then the user of the script doesn't need to care what interpreter is needed to run the script. See wikipedia shebang article.

    • I ran the following command to package the distribution:

      python setup.py sdist

    • After doing that, I was able to install the package and script by running

      sudo pip install test3-0.1.tar.gz

    • One thing I worried about was permissions on the script file. However, I noticed that distutils took care of this when packaging (changed mode to 755 or whatever).

    You can find my whole project for this example on github.

提交回复
热议问题