How to run python programs in pycharm with passing parameters? - python

I am currently having problem running python programs in pycharm, I generally used to right click and run the program. I want to run the program like this:
python hrllo.py rahulkapoo vit university
Here is the error I get:
Traceback (most recent call last):
File "C:/Users/rahul/PycharmProjects/untitled/hrllo.py", line 107, in <module>
aggregate()
File "C:/Users/rahul/PycharmProjects/untitled/hrllo.py", line 88, in aggregate
br = login()
File "C:/Users/rahul/PycharmProjects/untitled/hrllo.py", line 23, in login
response = br.open('https://academics.vit.ac.in/student/stud_login.asp')
File "C:\Python27\lib\site-packages\mechanize\_mechanize.py", line 203, in open
return self._mech_open(url, data, timeout=timeout)
File "C:\Python27\lib\site-packages\mechanize\_mechanize.py", line 255, in _mech_open
raise response
mechanize._response.httperror_seek_wrapper: HTTP Error 403: request disallowed by robots.txt
Process finished with exit code 1

In PyCharm, right-click on the script (or within the editor window that has the script open, click Create 'scriptname', put your params in the Script Parameters box near the top and click OK.
The new run config will appear to the left of the run button at the top of Pycharm (at least in my version it does). Now make sure that the new run config is listed when running the script.

Related

Problem running python cProfile in VSCode debug console

I'm trying to setup a good VSCode python environment for debugging including profiling using cProfile in a REPL manner, and I really like the debug widow.
In the VSCode debugger I am able to run/debug the following script:
import cProfile
cProfile.run("10 + 10")
print("Break here")
I am using the Microsoft "Python" extension for debugging and setting the output to "internalConsole" in my "launch.json" and the cProfile output correctly shows up in the debug console window.
However, after hitting a breakpoint set at print("Break here") and manually entering the command cProfile.run("10 + 10") into the debug console window, I get the following error:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python3.8/cProfile.py", line 16, in run
return _pyprofile._Utils(Profile).run(statement, filename, sort)
File "/usr/lib/python3.8/profile.py", line 57, in run
self._show(prof, filename, sort)
File "/usr/lib/python3.8/profile.py", line 72, in _show
prof.print_stats(sort)
File "/usr/lib/python3.8/cProfile.py", line 42, in print_stats
pstats.Stats(self).strip_dirs().sort_stats(sort).print_stats()
File "/usr/lib/python3.8/pstats.py", line 96, in __init__
self.init(arg)
File "/usr/lib/python3.8/pstats.py", line 110, in init
self.load_stats(arg)
File "/usr/lib/python3.8/pstats.py", line 136, in load_stats
raise TypeError("Cannot create or construct a %r object from %r"
TypeError: Cannot create or construct a <class 'pstats.Stats'> object from <cProfile.Profile object at 0x7efee11c30a0>
I am also able to run cProfile.run("10 + 10") in a command line python session as well as in the VSCode terminal ("integratedTerminal").
Any ideas on how to fix this and why the behavior is different for the debug console window?
I tried searching information regarding the mechanisms of the debug window in comparison with terminal window but have not found anything of substance yet.

Error during automated software install using pywinauto

After searching I am not able to find a good response:
I am trying to automate company software using pywinauto. I try to use app.print_control_identifiers() wherever possible. And it has been a God send! Thanks to folks on Youtube and Vasily's posts. See code below.
During the install, it wants to install a MS VC++ redistributable and I can handle that.
The next issue is telling me that the version of Adobe Flash Player (I know right?) is incompatible. I have used Inspect to ID the OK button I need to click to move on. Here is the code (cleaned up for public consumption):
from pywinauto.application import Application
import time
app = Application(backend="uia").start("C:\\Users\\me\\program.exe")
time.sleep(5)
#This addresses the need to install Microsoft C++ Redistributable
dlg = app['program - InstallShield Wizard']
dlg.Install.click()
time.sleep(5)
#This is to get past an install failure and to move on
dlg.Yes.click()
time.sleep(10)
#This is a dialog box saying that some component is incompatible and this is where I am stuck
# And the window title now just says 'program' vs. 'program - InstallShield Wizard'
new_dlg = app['program']
new_dlg.OK.click()
Here is the failure info:
C:\Python37\python.exe C:/Users/me/PycharmProjects/myProject/pywinauto_install.py
Traceback (most recent call last):
File "C:\Python37\lib\site-packages\pywinauto\application.py", line 258, in __resolve_control
criteria)
File "C:\Python37\lib\site-packages\pywinauto\timings.py", line 458, in wait_until_passes
raise err
pywinauto.timings.TimeoutError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/me/PycharmProjects/myProject/pywinauto_install.py", line 23, in <module>
new_dlg.OK.click()
File "C:\Python37\lib\site-packages\pywinauto\application.py", line 379, in __getattribute__
ctrls = self.__resolve_control(self.criteria)
File "C:\Python37\lib\site-packages\pywinauto\application.py", line 261, in __resolve_control
raise e.original_exception
File "C:\Python37\lib\site-packages\pywinauto\timings.py", line 436, in wait_until_passes
func_val = func(*args, **kwargs)
File "C:\Python37\lib\site-packages\pywinauto\application.py", line 203, in __get_ctrl
dialog = self.backend.generic_wrapper_class(findwindows.find_element(**criteria[0]))
File "C:\Python37\lib\site-packages\pywinauto\findwindows.py", line 87, in find_element
raise ElementNotFoundError(kwargs)
pywinauto.findwindows.ElementNotFoundError: {'best_match': 'program', 'backend': 'uia', 'process': 22184}
Process finished with exit code 1
I am creating new_dlg based on an SO post I saw, so if this is wrong, please slap my hand. I am not a trained developer.
Thanks
To check texts of the top level dialog windows print this:
print([w.window_text() for w in app.windows()])
Then just copy the correct one to your code as a dict key instead of app['program']. More flexible way looks so:
new_dlg = app.window(title_re="Program - .*")

idle crashing when I press configure idle

If I go on cmd and write python -m idlelib, that would open IDLE. If I were to then go on configure IDLE, on the cmd, it gives be an error
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\xxx\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "C:\Users\xxx\AppData\Local\Programs\Python\Python38-32\lib\idlelib\editor.py", line 574, in config_dialog
configdialog.ConfigDialog(self.top,'Settings')
File "C:\Users\xxx\AppData\Local\Programs\Python\Python38-32\lib\idlelib\configdialog.py", line 78, in __init__
self.create_widgets()
File "C:\Users\\AppData\Local\Programs\Python\Python38-32\lib\idlelib\configdialog.py", line 113, in create_widgets
self.highpage = HighPage(note)
File "C:\Users\xxx\AppData\Local\Programs\Python\Python38-32\lib\idlelib\configdialog.py", line 693, in __init__
self.load_theme_cfg()
File "C:\Users\xxx\AppData\Local\Programs\Python\Python38-32\lib\idlelib\configdialog.py", line 998, in load_theme_cfg
self.paint_theme_sample()
File "C:\Users\xxx\AppData\Local\Programs\Python\Python38-32\lib\idlelib\configdialog.py", line 1279, in paint_theme_sample
self.highlight_sample.tag_config(element, **colors)
File "C:\Users\xxx\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 3854, in tag_configure
return self._configure(('tag', 'configure', tagName), cnf, kw)
File "C:\Users\xxx\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1627, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: invalid color name "#00224"
I probably have a custom theme with an invalid color, however, how am I supposed to delete it if I can't access it (since it crashes when I do so).
There was a post, Python freezes when configuring IDLE that had I believe the same issue, but all he says as a solution is "manually deleting the faulty theme", which he doesn't explain. Pretty much, I want to access configure IDLE option without it crashing. Once it does crash, the only way I can close it then is by using task manager or restarting my computer.
As solution, you can tell me how to delete a custom theme without using IDLE if that's possible.
If the problem is in a custom theme, then the user configuration file you need to edit is $HOME/.idlerc/config-highlight.cfg. In your case, $HOME, based on the url below, appears to be C:/Users/chess. Just add a digit to make six. You can use the IDLE editor to do so. You should then be able to edit the color properly in the config dialog.
The problem might possibly be a character missing from the defaults file supplied with IDLE:
C:\Users\chess\AppData\Local\Programs\Python\Python38-32\lib\idlelib\config-highlight.def
Thank you for opening IDLE in a console, getting the traceback, and posting all of it. It gave me an idea of how to prevent the crash, so I opened a bug report

Python:Pycall Validation Error while Calling

I have Asterisk 13.20 set up and running fine on Ubuntu 16.04,calls are going well through a Softphone(Zoiper), I came across a Library in Python "Pycall" which lets you make calls through a Python Script.So I tried testing a snippet from the site Pycall
The code that I used:
from pycall import CallFile, Call, Application
call = Call('SIP/flowroute/18882223333')
action = Application('Playback', 'hello-world')
c = CallFile(call, action)
c.spool()
Just made changes in the extension,wherein I used one from my server
However I am getting the following error:
Traceback (most recent call last):
File "test.py", line 10, in <module>
c.spool()
File "/usr/local/lib/python2.7/dist-packages/pycall/callfile.py", line 131, in spool
self.writefile()
File "/usr/local/lib/python2.7/dist-packages/pycall/callfile.py", line 119, in writefile
f.write(self.contents)
File "/usr/local/lib/python2.7/dist-packages/pycall/callfile.py", line 114, in contents
return '\n'.join(self.buildfile())
File "/usr/local/lib/python2.7/dist-packages/pycall/callfile.py", line 96, in buildfile
raise ValidationError
pycall.errors.ValidationError
Tried searching online for the solution but coudnt find one,What am I doing wrong?
This question not related to PBX, it is just incorrect use of LIB.
General instruction how to solve ANY issue with availible source code.
You have error
File "/usr/local/lib/python2.7/dist-packages/pycall/callfile.py", line 96, in buildfile
Go that file and line, check code, add more debug if needed, fix your app.
Run as asterisk user.
sudo su asterisk -s /bin/bash
python
from pycall import CallFile, Call, Application
call = Call('SIP/flowroute/18882223333')
action = Application('Playback', 'hello-world')
c = CallFile(call, action)
c.spool()

Spyder: typing an opening bracket in console puts focus on internal console

I have recently re-installed spyder on ubuntu 12.04. Here is the problem:
When I type an opening bracket in the console, the focus switches immediately to the internal console, displaying an error (that I interpret as "missing closing bracket"?) While I appreciate the code monitoring, it would be nice if the console let me finish my line of code first before jumping to conclusions.
How can I avoid this behavior? (I dont mind the "error" showing in the internal console, but the switch to the internal console whenever I open a bracket is quite disruptive.)
Here is the output of the internal console:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/spyder-2.3.0-py2.7.egg/spyderlib/widgets/shell.py", line 298, in keyPressEvent
self.postprocess_keyevent(event)
File "/usr/local/lib/python2.7/dist-packages/spyder-2.3.0-py2.7.egg/spyderlib/widgets/shell.py", line 727, in postprocess_keyevent
ShellBaseWidget.postprocess_keyevent(self, event)
File "/usr/local/lib/python2.7/dist-packages/spyder-2.3.0-py2.7.egg/spyderlib/widgets/shell.py", line 438, in postprocess_keyevent
self._key_parenleft(text)
File "/usr/local/lib/python2.7/dist-packages/spyder-2.3.0-py2.7.egg/spyderlib/widgets/shell.py", line 797, in _key_parenleft
self.show_object_info(last_obj, call=True)
File "/usr/local/lib/python2.7/dist-packages/spyder-2.3.0-py2.7.egg/spyderlib/widgets/mixins.py", line 608, in show_object_info
signature=True, color='#2D62FF')
File "/usr/local/lib/python2.7/dist-packages/spyder-2.3.0-py2.7.egg/spyderlib/widgets/mixins.py", line 108, in show_calltip
self.calltip_widget.show_tip(point, tiptext, wrapped_textlines)
File "/usr/local/lib/python2.7/dist-packages/spyder-2.3.0-py2.7.egg/spyderlib/widgets/calltip.py", line 157, in show_tip
forward=False)
File "/usr/local/lib/python2.7/dist-packages/spyder-2.3.0-py2.7.egg/spyderlib/widgets/calltip.py", line 252, in _find_parenthesis
while category(char) != 'Cc' and position > 0:
TypeError: need a single Unicode character as parameter
To not show errors on the Internal console, you need to go to
Tools > Preferences > General > Debugging
and deactivate the option called
[ ] Pop up internal console when internal errors appear

Categories