Where do I see existing variables after executing my script? I have become accustomed to having a panel to see what existing variables I have and what their types and contents are in MATLAB and Spyder; how is this done in VSCode for Python?
During debugging, you can inspect all available variables on the left side of the screen as shown in the following screenshot (although after debugging they disappear)
Related
I run my script with Python Console in PyCharm and expect it to show global variables that are being used by script itself. But somehow it seems like it uses some different set to work with / different scope / namespace.
Here is a sample script:
def addb():
global b
b=1
def checkb():
print('b' in globals())
I go to 'Run > Edit Configurations... > myscript' and check 'Run with Python console' there, press Run - Python Console opens - check Show Variables button and list of some global variables appears on the right. Then I write in prompt and get this response:
>>>'b' in globals()
False
>>>checkb()
False
Everything fine, no 'b' variable in variables list on right pane as well. Then I write and get this response:
>>>addb()
>>>checkb()
True
>>>'b' in globals()
False
Also, still no 'b' variable in variables list on the right pane. And this goes further, of course. This variable seems to be clearly existing and have appropriate value for functions and methods inside module, but not for Python Console. This is the most obvious example of such behavior. It goes further and bring undesirable consequences when I use some functions to change global variables state and use them in console. The variables changes their value "inside" of script, but remain the same state in console, etc.
What am I expecting to have is all actual script global variables' state be visible in variables list on the right pane of Python Console and be accessible from console so
>>>addb()
>>>'b' in globals()
would yield
True
I checked this in Python own console directly with
python -i test.py
and had exactly what I expected to have like I described above. So it's exactly PyCharm Python Console issue.
It looks completely like a bug behavior, but I just can't believe it is since it is too big and obvious not to notice so I just believe I missed something and decided to ask here first. Can't find anything relating this case in Google as well so I just stuck here. Would appreciate any guide or confirmation of it's buggy nature so I can raise an issue. Please let me know if you can't reproduce this issue as well.
I use PyCharm 2018.3.4 (Community Edition) Build #PC-183.5429.31, built on January 29, 2019 and Python 3.7.2.
I recently started working in python with visual studio and I have 2 questions please:
Is there some kind of special configuration that you have to perform in order to be able to debug python code? Was I supposed to install something or did VS come this way?
Suppose im in a decoding session, inside a function that's declared in the scope of some class. Can I view the values of the class variables defined in the init? If so, how? I tried to write "self.someField" in the watch window but it didn't work.
Thanks you!
You could debug your python code like general debugging steps in VS, of course, it also has different settings or Environment options.
If you want to get much more detailed information, please see this document here:
Debugging your Python code
In addition, one thread for one issue, for the second issue:
2.Suppose im in a decoding session, inside a function that's declared in the scope of some class. Can I view the values of the class
variables defined in the init? If so, how? I tried to write
"self.someField" in the watch window but it didn't work.
If possible, I suggest you open a new case for this new issue, you could also share your main code, so other community members could really repro this issue in their side.
I've been searching everywhere for an answer to this but to no avail. I want to be able to run my code and have the variables stored in memory so that I can perhaps set a "checkpoint" which I can run from in the future. The reason is that I have a fairly expensive function that takes some time to compute (as well as user input) and it would be nice if I didn't have to wait for it to finish every time I run after I change something downstream.
I'm sure a feature like this exists in PyCharm but I have no idea what it's called and the documentation isn't very clear to me at my level of experience. It would save me a lot of time if someone could point me in the right direction.
Turns out this is (more or less) possible by using the PyCharm console. I guess I should have realized this earlier because it seems so simple now (though I've never used a console in my life so I guess I should learn).
Anyway, the console lets you run blocks of your code presuming the required variables, functions, libraries, etc... have been specified beforehand. You can actually highlight a block of your code in the PyCharm editor, right click and select "Run in console" to execute it.
This feature is not implement in Pycharm (see pycharm forum) but seems implemented in Spyder.
I am using PyCharm but I do not like default code completion which shows _protected and __mangled names higher than public.
How can I force to show names starting with underscore at the bottom of code completion list and public at the top?
Some editors like PyDev is doing it much better without configuration.
Screen asked by #jole showing my problem:
This is not configurable in PyCharm; there is an open issue for changing the default behavior of the completion in this context.
I have successfully achieved this using the method documented at Run IPython Notebook in Iframe from another Domain . However, this required editing the user config file. I was really hoping to be able to set this up via the command-line instead (for reasons).
http://ipython.org/ipython-doc/1/config/overview.html indicates that configuration via the command line is possible. However, all the examples are for simple true/false value assignment. To set the server up to allow embedding, it is necessary to set a value inside a dictionary. I can't work out how to pass a dictionary in through the command-line.
Another acceptable option would be a configuration overrides file.
Some people will wonder -- why all this trouble!?!
First of all, this isn't for production. I'm trying to support non-developers by writing a web-based application which integrates Ipython notebooks within it using iframes. Despite being on the same machine, it appears that the different port number used is enough to mean that I can't do simple iframe embedding without setting the x-frame insecurity bit.
Being able to do this via the command line lets me set the behaviour in the launch script rather than having to bundle a special configuration file inside my app, and also write an installer.
I really hope I've make the question clear enough! Thanks for any and all suggestions and help!
Looking over the IPython source for the loaders, it seems like it will execute whatever python code you put on the right hand side. I've not tested it, but based on the link you provided, you can probably pass something like
--NotebookApp.webapp_settings=dict('headers'=dict('X-Frame-Options'='ALLOW-FROM https://example.com/'))