r/inventwithpython • • Dec 26 '17

[Automate] Alternative way of launching an application from within Python on mac OS

4 Upvotes

Chapter 15 of "Automate the Boring Stuff with Python" discusses really cool stuff like multithreading, concurrency, and subprocesses. I really enjoyed studying it like I did the first fourteen chapters.

Yet, I believe other programs can be started on mac OS similar to how they can be in Windows contrary to what is stated under the “Launching Other Programs from Python" and “Opening Files with Default Applications” headings in this chapter.

You don't need to pass the ‘Open’ command and the application’s name to start an application in mac OS via Python. You just need to pass its executable file's absolute path to subprocess.Popen(), similar to how it would be done under Windows. Below is how the calculator application, for example, can be opened on mac OS from within Python.

import subprocess
calcProc = subprocess.Popen('/Applications/Calculator.app/Contents/MacOS/Calculator')
# Below expression should return “True” if calculator is launched and running
calcProc.poll() == None

True # Correct value

The “default” application opening method recommended in the book (shown below) for mac OS has also the disadvantage of being non-trackable as far as I can see.

import subprocess
calcProc = subprocess.Popen(['open', ‘/Applications/Calculator.app/'])
# “False” value by expression below means calculator is not launched or is closed  
calcProc.poll() == None

False # Contrary to what this outcome implies the calculator is launched and not closed

To find the path to an executable app/program file in mac OS, CTRL-click the application and select Show Package Contents. Locate the “MacOS” subfolder through the opened Finder window. This subfolder should have the executable program file in it. CTRL-click the executable file and select Get Info. In the window that opens, you can get the path to the executable file from Where under General. Just copy and paste what is shown next to Where to your Python editor. Add a forward slash and the name of executable file as shown in the Finder window to it without any extensions.

The path may come out in a different (but the correct) format when pasted to your Python editor from how it might be shown in Finder's Get Info window. Also note that Python / mac OS is partly sensitive to the capitalization of the path. Therefore, do not change capitalization of any of the letters in the path after pasting it. If you make any capitalization changes, Python may not be able to locate the executable file.

This way of locating an executable for mac OS is partly mentioned under the “Launching Other Programs from Python" heading but for some reason the book, thereafter, recommends using and describes the “default” method.

Pls let me know if you have encountered otherwise when studying this chapter using mac OS or have any other suggestions. Thank you.


r/inventwithpython • • Nov 22 '17

Automating Gmail with Inconsistent results

4 Upvotes

Has anyone else experienced the problem of the script running only once then only going halfway the second time. I noticed the selectors keep changing whether I use css_selectors or xpath to locate the message body and 'send' button. TIA.


r/inventwithpython • • Jan 08 '17

Python newb question.

4 Upvotes

Alright so I decided to try python as the first language to learn. I'm very excited as I've tried to learn c++ and lost motivation really quickly. I'm hoping python will help me get the bigger picture without worrying about syntax issues. Anyway so I'm running an old 2011 MacBook Pro (unfortunately) and have Windows 10 partioned 50/50. I've tried to start using python in both operating systems but can't seem to get either to work. In Windows I can't get past the path variable error (already did path;python3.6 location) and in mac using 2.7 and I can't get scripts I write to run in terminal (keeps popping up syntax errors). I'm using bracket by adobe as a substitute for notepad++. Preferably I'd like to run it on windows however getting it running on either operating system would be awesome at this point.


r/inventwithpython • • Dec 31 '16

Memoization a function to make it run faster?

4 Upvotes

I am trying to Memo this function http://pastebin.com/n0KkE2Yr I have no syntax errors, but the output is very strange. I think it has to do with all the recursion going on, but I am not sure. Can someone explain to me what's wrong?


r/inventwithpython • • Dec 03 '16

Automate the Boring stuff Flashcards

4 Upvotes

Does anyone know of flashcards for Automate the Boring Stuff?

I am mostly interested in flashcards that have the functions and methods used in each chapter.


r/inventwithpython • • Aug 23 '16

[Automate] Problem with Selenium & Firefox

4 Upvotes

I'm going through Chapter 11 - Web Scraping, and I just began the section that introduces Selenium. The chapter instructs us to enter the following into the python interpreter:

>>> from selenium import webdriver
>>> browser = webdriver.Firefox()
>>> type(browser)
<class 'selenium.webdriver.firefox.webdriver.WebDriver'>
>>> browser.get('http://inventwithpython.com')  

Right after I enter "browser = webdriver.Firefox()" Firefox opens, but disables (Im not sure if this is the right word) my terminal. I am no longer prompted with the ">>>" in the interpreter, and therefore unable to continue with the "type(browser)" command.

After maybe 30 seconds - 1 minute, Firefox closes and I get this error:

selenium.common.exceptions.WebDriverException: Message:  
Can't load the profile.Dir [file path]...If you specified a log_file 
in the FirefoxBinary constructor, check it for details.

Im using OSX, selenium version is 2.53.6, and python version is 3.5.2.

Thanks for the help.


r/inventwithpython • • Mar 13 '16

Disabling assertions in 'Automate the Boring Stuff'

4 Upvotes

On page 244 of 'Automate the Boring Stuff', there's a section with the heading 'Disabling Assertions'.

The final sentence of this section is: 'See Appendix B for details about how to launch your probably-not-insane programs with the -O option.'

However (in my copy, at least), there's no mention of this in Appendix B.

Is the information printed elsewhere in the book, or was it accidentally omitted?


r/inventwithpython • • Feb 10 '16

Automate the Boring Stuff - Chapter 7 Strong Password Protection.

4 Upvotes

I wanted to post my code and see if there was a better way I could've used regex w/o using a positive lookahead. That concept wasn't introduced in the chapter but every solution for this practice project uses it.

Also, love the book so far. For some reason I've never understood regex, but Ch. 7 just clicked for me.

import re

capReg = re.compile(r'.*[A-Z].*')
lowerReg = re.compile(r'.*[a-z].*')
digitReg = re.compile(r'.*\d.*')


def checkPassword(text):
    if capReg.search(text) and lowerReg.search(text) and digitReg.search(text):
        return True
    else:
        return False

pw = 'kenIsgreat99'
print(checkPassword(pw))

Edit: Instructions from the book. Write a function that uses regular expressions to make sure the password string it is passed is strong. A strong password is defined as one that is at least eight characters long, contains both uppercase and lowercase characters, and has at least one digit. You may need to test the string against multiple regex patterns to validate its strength.


r/inventwithpython • • Sep 28 '15

Problem with a project from Automate The Boring Stuff

4 Upvotes

Edit: Code has now been fixed.

I've created the randomQuizGenerator from Auomate the Boring stuff chapter 8 (https://automatetheboringstuff.com/chapter8/). It should create 70 papers, 35 quiz sheets, 35 answer sheets.

It does that, but they're largely blank and I can't seem to figure where I am going wrong?

Any help would be much appreciated. My code is in the link below;

https://github.com/Millsy88/AutomateTheBoringStuff/blob/master/RandomQuizGenerator.py

Thanks!


r/inventwithpython • • Jun 03 '15

Automate The Boring Stuff - Chapter 5 - Why do my dictionaries always print in a different order?

4 Upvotes

Doing the practice problems in Chapter 5, I've noticed that when I run the program multiple times the output is different. The values are all the same, but in a different order each time.

Here's an example: this program displays the original inventory, then calls the function that adds loot to it, then displays it again.

https://gist.github.com/anonymous/8a2abef5024999270d1a

Here's my code:

https://gist.github.com/anonymous/7001bde7c602016234cb


r/inventwithpython • • May 26 '15

(ATBSWP Ch. 6) I completed the Practice Project, but I have a question about a for loop I made.

4 Upvotes

I finished Chapter 6's Practice Project, which asks you to 'Write a function named printTable() that takes a list of lists of strings and displays it in a well-organized table with each column right-justified.'. Here is how I made it. The function takes the tableData list of lists and prints an ordered version of the items and, just for testing reasons, the colWidths list, and it looks like this:

    apples Alice  dogs
  oranges   Bob  cats
 cherries Carol moose
   banana David goose
[9, 6, 6]

While the code works, the version I had made before it didn't work correctly. It was the same, except in the if colWidths[a] <= len(table[a][b]): I had written if colWidths[a] < len(table[a][b]):, and it printed this:

   apples Alice dogs
  oranges   Bob cats
 cherries Carolmoose
   banana Davidgoose
[9, 6, 5]

My question is: Why is the outcome different? Why does the colWidths[a] = len(table[a][b]) + 1 only adds one if the if statement is has the <= comparison operator and not with <?

I am sorry if my question wasn't clear enough, it's my first one. Thanks in advance.


r/inventwithpython • • Apr 07 '15

Help importing pygame: ImportError: No module named 'pygame.locals'

4 Upvotes

Hello. I'm on the Graphics and animations chapter and I keep trying to run the code but I get error above when I run it. I'm running python on 64-bit ubuntu, is it possible that I've installed either 32 bit python or pygame, and that is what caused the issue? If so how would I check?


r/inventwithpython • • Mar 31 '15

Playing around with this book, am stuck with an error I can't figure out!

4 Upvotes

import random HANGMANPICS = ['''

+---+ | | | | | | ==========''','''

+---+ | | O | | | | ==========''','''

+---+ | | O | | | | | ==========''','''

+---+ | | O | /| | | | ==========''','''

+---+ | | O | /|\ | | | ==========''','''

+---+ | | O | /|\ | / | | ==========''','''

+---+ | | O | /|\ | / \ | | ==========''',''']

I assume it is something to do with my quote placements, it keeps giving me an EOL error can someone point me in the right direction?


r/inventwithpython • • Jul 29 '14

Sharing your python creations online

4 Upvotes

Is there a site where I could post my pygame creation so that a group of friends could play it?


r/inventwithpython • • Sep 28 '13

Make python/pygame script into an .exe

4 Upvotes

I am wondering where a good place would be to learn to compile the games we create using pygame into an .exe maybe even a .dmg? I have been looking into cx_freeze, is this a good option? Have you made any tutorials on the subject?

Thank heaps!


r/inventwithpython • • Aug 12 '13

Ask r/inventwithpython: What types of problems learning to program do you commonly face?

4 Upvotes

Just a question for people learning from the "Invent with Python" books. What are some programming problems that you face, or other difficulties that you've had, when trying to learn from a book?


r/inventwithpython • • Jun 18 '13

newer version of Python than used in the book

4 Upvotes

The book says to use Python 3.1, but the current version of Python is 3.3.2. Will that cause any coding issues?


r/inventwithpython • • May 10 '13

When will the .mobi version of the book be complete?

4 Upvotes

Sometime soon. Currently I have the books as Word documents and I need a way to convert them to .mobi or .epub formats. Currently the easiest way to do this seems to save the Word docs as HTML, and then use the Kindle publishing software to create epubs from the HTML, though this results in really messy-looking epubs.

If anyone has suggestions for a different workflow please email me at [email protected]


r/inventwithpython • • May 09 '13

Errors when copying the code? Use the online diff tool to see the difference between your code and the code in the book.

Thumbnail inventwithpython.com
4 Upvotes

r/inventwithpython • • May 09 '13

I get an error like "NameError: name 'john' is not defined" for the first program.

4 Upvotes

This is caused by using Python 2 instead of Python 3. The input() function does something different in Python 2 than it does in Python 3.


r/inventwithpython • • Oct 08 '26

Brython Offline - A single .html file with a Python interactive shell

Thumbnail github.com
3 Upvotes

r/inventwithpython • • Jun 25 '25

Error trying to install automateboringstuff3 package in Python 3.13.4

3 Upvotes

I'm following the newest 3rd edition of Automate the boring Stuff with Python and now I'm in chapter 12 where it shows you how to create a venv and install all the packages that will be used by the book with its versions. But I get an error when I try to install the full package via python -m pip install automateboringstuff3 https://pastebin.com/7TBwhNP3 I've tried to nuke the venv and start over installing each package individually and everything installs fine except python -m pip install playwright==1.47.0 (https://pastebin.com/pUeDNzNE).

Apparently there's a problem with greenlet. ChatGPT says that it's due from the package being old and my python being to modern, so it suggest to downgrade python to 3.11.x but before trying that I'd like to ask here, because in the book Al's seems to be using 3.13.

SOLVED (partially): If you use Python 3.12x you can manually install every package. The automateboringstuff3 still not working (and you don't need to install Microsoft visual C++).


r/inventwithpython • • Jul 30 '24

what is wrong with the code

3 Upvotes

Hello. I took that code from practice questions in chapter 2 of "Automate the Boring Stuff with Python Practical Programming" (Al Sweigart). I wrote exactly how it should be, but the MU says that I have a mistake in line one.

Can someone explain what is wrong?

if spam == 1:

print('Hello')

elif spam == 2:

print('Howdy')

else:

print('Greetings')


r/inventwithpython • • Dec 21 '23

Google authorization despair

3 Upvotes

Hi! I've been using EZsheets etc for automating a twice annual reporting process with Google sheets, mail, and drive... which collides into the 180 day expiration of Google authorization keys.

This go around, I've found my efforts to reauthorize -- including creating a new "app" -- foiled in the end by the unhelpful error message:

google.auth.exceptions.RefreshError: ('invalid_grant: Bad Request', {'error': 'invalid_grant', 'error_description': 'Bad Request'})

Any advice on what I can try to get this working? I promise to run the app monthly henceforth to keep my access current. Thanks!


r/inventwithpython • • Dec 17 '23

Can't find author.com server

3 Upvotes

Hi, Al. Reading Automate the Boring Stuff. I've tried linking to https://author.com/vampire2, among others. My browser returns a message saying it's unable to find the server. Is there a problem with the site? Thanks.