admin管理员组

文章数量:1379588

#!/usr/bin/env python3

# interact with command line, open a webpage, grab what you have in your clipboard
import sys
import webbrowser
import pyperclip

# function allowing us to open google maps page
def print_addy(address):
    webbrowser.open(f'/{address}')
# checking to see if we passed an argument on the terminal if not grab what's copied in clipboard
if len(sys.argv) > 1:
        address = ' '.join(sys.argv[1:])
else:
    # ## MODULENOTFOUND error in MAC CMD line, TypeError: missing argument in IED CMD line
    address = pyperclip.paste()

# call the function
print_addy(address)

It works when I hit the run button and have an address copied or pass in an argument. However, when i try to run it on the Pycharm terminal with ./mapIt.py 123 random st

I get

    Traceback (most recent call last):
  File "./mapIt.py", line 17, in <module>
    print_addy()
TypeError: print_addy() missing 1 required positional argument: 'address'

Why is it not recognizing the argument I'm passing?

#!/usr/bin/env python3

# interact with command line, open a webpage, grab what you have in your clipboard
import sys
import webbrowser
import pyperclip

# function allowing us to open google maps page
def print_addy(address):
    webbrowser.open(f'https://www.google/maps/place/{address}')
# checking to see if we passed an argument on the terminal if not grab what's copied in clipboard
if len(sys.argv) > 1:
        address = ' '.join(sys.argv[1:])
else:
    # ## MODULENOTFOUND error in MAC CMD line, TypeError: missing argument in IED CMD line
    address = pyperclip.paste()

# call the function
print_addy(address)

It works when I hit the run button and have an address copied or pass in an argument. However, when i try to run it on the Pycharm terminal with ./mapIt.py 123 random st

I get

    Traceback (most recent call last):
  File "./mapIt.py", line 17, in <module>
    print_addy()
TypeError: print_addy() missing 1 required positional argument: 'address'

Why is it not recognizing the argument I'm passing?

Share Improve this question edited Mar 25 at 3:08 Erick Chico asked Mar 19 at 3:48 Erick ChicoErick Chico 212 bronze badges 14
  • 1 please post the full error message including the stack trace – juanpa.arrivillaga Commented Mar 19 at 4:07
  • 1 Are you sure you're running the same version of the script? If you use cat ./mapIt.py do you see the argument in the function call? – Barmar Commented Mar 19 at 4:24
  • I don't see in your question, how you run it from the terminal. – user1934428 Commented Mar 20 at 9:30
  • @juanpa.arrivillaga just updated thank you! – Erick Chico Commented Mar 25 at 3:15
  • 1 You're running a different version of the script when you run it from the terminal than from PyCharm. Probably the PyCharm version is in a different directory than your terminal working dir. – Barmar Commented Mar 25 at 14:27
 |  Show 9 more comments

2 Answers 2

Reset to default 2

This was a saving issue... I assumed my script file was being autosaved it was not. I opened the actual script file and updated it and it works as intended. Hope this helps someone else thats new to python!

Couple of pointers here:

  1. You should probably wrap your indentation level 0 code in a if __name__ == '__main__' block (only execute if your script is being called directly, read this to understand more) to avoid calling your script accidentally and maybe cause your error.
  2. To evade the problem, you could provide a default argument to your function and only execute the webbrowser code if the parameter is valid.

Something like this should work:

import sys
import webbrowser

import pyperclip


def print_addy(address: str = "") -> None:
    if address:
        webbrowser.open(f'https://www.google/maps/place/{address}')


if __name__ == '__main__':
    if len(sys.argv) > 1:
        address = ' '.join(sys.argv[1:])
    else:
        address = pyperclip.paste()

    print_addy(address)

For any more help we need the full stack trace like juanpa said.

本文标签: