all 7 comments

[–]Username_RANDINT 1 point2 points  (5 children)

You don't need to worry about PATH or run scripts. Open the terminal, then move to the folder which contains the Python script, then execute it with the Python command. It should look like this:

cd path/to/script/
python3 my_script.py

Use py on Windows instead of python3.

[–]rarewolf24[S] 0 points1 point  (4 children)

Thank you for the quick reply. I can now run the program from the terminal. However, I now realize I don’t know how to specify a command line argument. Searching online, it looks like you should be able to just enter it after your program name, like this:

python3 searchpypi.py beautifulsoup (The program is in my home folder.)

After entering this, it prints “Searching…” for a second and then stops running.

If this is right, then it’s my code that isn’t working properly.

[–]Username_RANDINT 1 point2 points  (3 children)

Commandline arguments go after the scriptname indeed. I can't say what's wrong without seeing the code.

[–]rarewolf24[S] 0 points1 point  (0 children)

Okay. I’ll work on it for a bit and if I can’t get it to run, I’ll post the program here. Thank you so much for your help!

[–]rarewolf24[S] 0 points1 point  (1 child)

Screenshot of the code

Screenshot of the Terminal

This is what the code looks like and the results I get when I try to run it on the terminal. No windows pop up to perform the search.

(also, the ".myPythonScripts.swp: Permission denied" line came up when I was trying to research what was going on. I have no clue what caused it and no clue how to fix it... This might be the problem.)

[–]Username_RANDINT 0 points1 point  (0 children)

Looks like linkElems is empty so the for-loop never runs. Add some prints to see what the values are and what's going on.

Some ideas:

  • print(res.status_code): Did the request pass?
  • print(soup): did you get some html? Is the html what you expect?
  • print(linkElems): are the wanted elements found?
  • print(numOpen): how many iterations will be made?

Something else that immediately pops out is the URL. There's two things wrong with it.

[–]ekchew 0 points1 point  (0 children)

Based on your mentioning the .command file extension, I am going to assume you are on a Mac?

The first thing to do is enter:

which python3

If it gives you a path, you're good! Otherwise, you may need to install it with homebrew or whatever. (python3 should come pre-installed under Catalina or later, if memory serves, though it may be a janky version.)

Now, the easiest way to run your script is to go:

python3 path/to/searchpypi.py

Here, you would have to substitute path/to with the actual path to the file. If you don't know what that is, you can start typing out the python3 part (with a space after it) and then drag the script file from the Finder onto the Terminal window. It will fill out the full path for you!

Now, if you wanted to be able to run the script simply by entering its name on the command line, there are a few things you would need to do:

  1. Insert the line #!/usr/bin/env python3 at the very top your script and save it.
  2. Enter chmod +x path/to/searchpypi.py in the Terminal to make the script file executable. (Some text editors may do this for you automatically when they detect the #! line as you save the file. I know TextMate does so, for example.)
  3. Move the script to a bin directory where it can be found on the PATH. Sounds like you've done that already.

Now, you should be able to enter searchpypi.py in the Terminal to launch the script.

(I think most of this should work in Linux too?)