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 | Show 9 more comments2 Answers
Reset to default 2This 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:
- 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. - 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.
本文标签:
版权声明:本文标题:python - My code works when I run it by simply hitting the run button in Pycharm but not on terminal? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744481308a2608184.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
cat ./mapIt.py
do you see the argument in the function call? – Barmar Commented Mar 19 at 4:24