2

I am currently stuck on exercise 46 in learn python the hard way. I was able to do everything without any issues up until the required quiz.

Here are the questions for the required quiz:

  1. Read about how to use all of the things you installed.

  2. Read about the setup.py file and all it has to offer. Warning: it is not a very well-written piece of software, so it will be very strange to use.

  3. Make a project and start putting code into the module, then get the module working.

  4. Put a script in the bin directory that you can run. Read about how you can make a Python script that's runnable for your system.

  5. Mention the bin script you created in your setup.py so that it gets installed.

  6. Use your setup.py to install your own module and make sure it works, then use pip to uninstall it.

I did questions 1 - 3 without issues but I do not know what to do for the other questions. I tried to read about how to make a python script that's runnable for my system but could not find anything. All the results talked about executable script. Is this the same as a runnable script? Also, how do I install a script using setup.py

here is my module called math.py:

print "5 + 5 is %d" % (5 + 5)

and here is my setup.py:

try:
    from setuptools import setup
except ImportError:
    from distutils.core import setup

config = {
    'description': 'Simple addition project',
    'author': 'Nathan',
    'url': 'URL to get it at.',
    'download_url': 'Where to download it at.',
    'author_email': 'nathanralph33@gmail.com',
    'version': '0.1',
    'install_requires': ['nose'],
    'packages': ['math'],
    'scripts': [],
    'name': 'math.py'
}

setup(**config)

I am a beginner to python programming so I apologize for my question. I have no idea what to do for this part of the exercise. Any help will be much appreciated. Thanks in advance

Sorry for any spelling/grammatical errors.

My operating system is windows 10 and I am using windows powershell as a terminal.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ralpher01
  • 141
  • 2
  • 3
  • 10

4 Answers4

3

Assuming this would be for a POSIX compliant OS (Linux, UNIX, BSD, etc), you would likely want to have two things:

shebang at the start of the python file:

#!/usr/bin/env python

And have the correct permissions for the file:

chmod +x file.py

Which would make the file executable.

After this is completed, you can ./file.py from a terminal to view your output.

Note: Please add your operating system information to your question.

Exodus
  • 49
  • 1
  • 3
  • I am very sorry for not including that. I am using windows 10 and I am using powershell as a terminal. – ralpher01 Nov 26 '16 at 01:22
  • 2
    do I put #!/usr/bin/python and chmod +x file.py at the top of the setup.py file? – ralpher01 Nov 26 '16 at 01:23
  • 2
    On POSIX systems one would normally see ``#!/usr/bin/env python`` rather than ``#!/usr/bin/python``. Using ``#!/usr/bin/env python`` means it will pick up ``python`` executable from ``PATH`` rather than it being hardwired. Picking it up from ``PATH`` makes it possible use Python virtual environments. – Graham Dumpleton Nov 26 '16 at 02:29
  • Alright, thanks. Do I put this in the setup.py file or the math.py file? – ralpher01 Nov 26 '16 at 02:38
  • Also, what should I put into the script that I am supposed to put in the bin directory and what is the difference between a script and a module? Sorry for all the questions i am just very lost on this exercise. – ralpher01 Nov 26 '16 at 02:55
  • Since you seem to be following some course material, why not go back and review the course material again. If they are asking you those questions, presumably they provide all the information you need. – Graham Dumpleton Nov 26 '16 at 05:43
1

I'm doing the same exercise and I have been struggling with these for several hours. I think here is the way that also works in Windows10 (I'm using Windows7):

  1. Put your math.py in bin derectory.

  2. In your setup.py,

    config = {
         ...
        'scripts': 'bin/math.py'
         ...
    }
    
  3. Now, in the Powershell, move to your MATH project directory (where the setup.py exists), and type in

    python setup.py install 
    

    Then the MATH will be installed (with the math.py in the bin directory).

  4. Now try this in Powershell:

    math.py
    

    You can get your answer for "5 + 5" and this command can be run no matter which directory you are, because you have installed it.

  5. Type in Powershell:

    pip list
    

    You can see your project installed.

  6. Type in Powershell:

    pip uninstall <your project name>
    

    You can uninstall it.

0

I got stuck in the same place, and found the answer in another thread:

On Windows, the standard Python installer already associates the .py extension with a file type (Python.File) and gives that file type an open command that runs the interpreter (D:\Program Files\Python\python.exe "%1" %*). This is enough to make scripts executable from the command prompt as foo.py. If you’d rather be able to execute the script by simple typing foo with no extension you need to add .py to the PATHEXT environment variable.

Here is the link: https://stackoverflow.com/a/4235923/9260134

0

I got stuck here as well i found this little nugget of information that zed doesn't mention.

your directory structure

skeleton/
     NAME/
         __init__.py
         yourmodual.py # must not be the same as the NAME of module
     bin/
     docs/
     setup.py
     tests/
         NAME_tests.py
         __init__.py # delete this for nosetests to work 

also config file as mentioned above

config = {
    'packages': ['math'],
    'scripts': ['bin/not_math.py'],
    'name': 'math'
}

So there is a couple of things going on here, correct naming of module files and declaring in scripts if you want to run from system

found here module has no attribute and on lpthw forum https://forum.learncodethehardway.com/t/ex46-nosetests-ran-0-tests-or-ran-1-tests-with-error/3869/22