How to use VS Code - Python Shell - python

So I am relatively new to programming and used to use Python's own "IDLE". After I run a ".py" file with IDLE, I am used to getting a python shell or a command window, I don't really know the terminological name for it, where I could play around with the objects inside the script.
For example, if I had a list A=[1,2,3] inside the program, after I run it I get a command console that says ">>" and I can say ">>A" which gives me "[1,2,3]" and I can add elements to A etc.
Now, I want to start using VS Code but I can't seem to find that particular thing. I have a terminal where I can run python code if I give the command "python" first, but It doesn't seem to effect anything inside the script.
I want to use that to see if some objects are working fine and to check their types etc. I add more lines to code after I try from there first, if that makes sense.
Sorry for really bad terminology, I really don't know the names but I can try even more if it's not clear.
Thanks a lot in advance.

Are you looking for the Integrated Terminal of VS Code?
Here are some ways to open the terminal:
Use the ⌃` keyboard shortcut with the backtick character.
Use the View > Terminal menu command.
From the Command Palette (⇧⌘P), use the View: Toggle Integrated Terminal command.
In the window that shows up, enter python and you'll get the Python shell you're looking for.

Try using the integrated terminal inside vs code and make sure that python and pip are properly configured. Type python in the command line and make sure the terminal points to the same folder where your program file is located.

Related

How do I stop Python from running code right after I type it in?

I just started to code on Python, so I downloaded the "Python 3.10 (64-bit)". The problem is that it runs the code without my permission. For example, if I am trying to type in print("example") it just runs on it's own. This means I can't do an actual coding project, it just runs the 1 piece of code right after I hit the Enter button. I don't know if I'm just blind or it's obvious, but I can't figure it out. What do I do?
Hitting enter after you type is "giving permission" to run the code that is typed (or copied)...
You can still write "scripts" this way - you define variables, functions, classes, etc, and they remain in scope of the interactive session.
If you want to "code a project," though, you should use an external IDE such as Visual Studio Code, PyCharm, Spyder, etc. The built-in IDLE is very basic, but still does allow for file execution.
Where do you write your code?
If you are using the command line, you cannot do anything to change this; you have to use a file with the extension ".py", for example "first_file.py", and write the code inside that file. When you are done or when you want to test your code, you have to open the terminal in the file folder and type python [filename].py and after that, your code will be compiled and executed in the terminal. I recommend using a code-writing program like VS Code, Atom or something like that.

Get Python Shell open

So i open Python 3.7.3 and all i get is a black window that looks like the command prompt window. Where is the shell? Where do I write my code? I saw an example with this same version of python where there were 2 white windows, one with the code and the other being the shell but i am confused how to set that up.
Is there a difference between just python 3.7.3 and IDLE? As you can see I am basically lost in this whole situation.
The window that I see looks like this: https://imgur.com/a/rMOMEh5
You have a few options, to name the basic ones:
Write your python code as a plain text file with an editor of your choice, and execute using:
python myfile.py
Write your python code in a simple IDE (e.g. sublime, atom) that has fancy features such as auto-indentation and colour coding, and execute using:
python myfile.py
Write your code in a fancy IDE that allows you to have shell and code at the same time, and execute within the IDE (e.g. PyCharm)
That's the python shell where you can issue python commands. You can call a python script as stated above and even code python directly in that window. Here is an example:
Type from secrets import token_urlsafe and hit return. Now you've imported your first python module.
Type ran_num = token_urlsafe(16) and hit return. Now we've declared a variable called ran_num and gave it the value of 16 random number, letters, and characters.
Type print(ran_num) and look at the output, which should be something like this - W2Z6O4XGebDa-eXbJY5p3Q
This is just one example. There are a lot of uses.

How to save code at the python prompt in the terminal to a local file

I have just written a bunch of lines of code on the Python prompt at the terminal. Now, I want to save all those lines of code to a .py file.
I am unable to find out how to do that. The only thing that I could find on StackOverflow was this answer but it shows only how to do it in an iPython notebook. I am not using an iPython notebook. I am running the code at the command line on the terminal.
I tried to follow that answer (because just in case) and ran the %save magic command on the terminal but it gave a SyntaxError.
So, how do save it?
Thanks!
See http://blog.e-shell.org/174 . As wu explains, the python prompt is using readline, and you can import a Python library to access this.
>>> import readline
>>> readline.write_history_file('/path/to/history.txt')
You can trying using another interpreter : bpython , I belive it has what you need,check it out.
Save the code you've entered to a file.
You seem to be affected by the misconception, that the python environment is workspace-centered (similar to what I know from Smalltalk and some LISP variants):
fire up with an initial workspace
modify by your liking
store the result
This is unfortunately not the case. While you can import existing files, the other option is to specify an existing file as initially to be loaded and keep the interpreter open by using the -i option.
It really depends on your terminal for the exact commands.
The general idea is to copy everything (if possible) or one page at a time from the terminal into a text editor and then clean the >>> prompts (and possibly other formatting problems) in the text editor.
But anyway, typing a lot of commands directly in the execution environment if really bad practice. At least you test a handful of lines and immediately save them in a file. IDLE is great at this game...

In Visual Studio Code, how do I load my python code to a read-print-eval loop?

I am teaching a class that uses VScode.
I am used to teaching using IDLE, and it is very nice for the students to be able to call their defined functions and run snippets of code in a python terminal, for debugging purposes.
In VScode, they I have been unable to do the same in a satisfactory way.
Option1: I can select all code, right click and run selection/line on terminal. This works for small snippets, but I cannot do it for the whole file (even after selecting the whole file with ctrl-A). On linux, this works, but on windows, it does not, unfortunately (and my students use windows)
Option2: I can use the debug console. This requires adding a breakpoint in one of the last lines of the file, and does not offer tab completion. It works, but is less convenient than IDLE.
Option 3: I can also add the commands to run to the bottom of the file (which is a least preferred alternative, given that is forgoes the interativity of the read-print-eval loop).
Is there any better solution? Installing a VScode extension would not be a problem.
Visual Code is just a text editor like your traditional notepad. to run and debug any kind program you need to install the particular extension for the programming language.
In your case you are using python so you need to install the extension of it. the best one is the "Python" which is developed by microsoft itself. go to your extensions manager and install this extension. right click and click "run python file in terminal" and you are all set.
this will run exactly as they run from the idle(which is default IDE provided by python itself) you can enter the arguments from the console itself. according to me this is the best way to run and debug python programs in VScode.
another way is that VScode shows which python version is installed on your computer on the left bottom side, click on it and the programs will use this interpreter.
out of all the ways listed here and many others, the best method is to run the program in the terminal which is the recommend by python itself and many other programmers.
this method is very simple. what you have to do is open up your command prompt and type the path where python.exe is installed and the type the path of the your program as the argument and press enter. you are done !
ex : C:\Python27\python.exe C:\Users\Username\Desktop\my_python_script.py
You can also pass your arguments of your program in the command prompt itself.
if you do not want to type all this and then just use the solution mentioned above.
hope that your query is solved.
regards

How can I set up "test" environments in Python

Is there a way to execute a file but still be able to call the variables? This would help immensely in learning Python, since I could set up many different "practice" scripts and then use the console to try and modify the code to see what happens.
I think the technical question would be "How can I populate a namespace in Python in Eclipse and leave it open to interactions in the console?
Below is a screenshot showing what I'm trying to do, define a (as 1) in the editor, and then press "ctrl-alt-enter" to run the file in the console, and then call the variable from the console.
[Screenshot of Eclipse]
You should be able to add a debugging breakpoint at the end of code you are executing and inspect variables using a good editor.
In general, you should be able to run simple scripts right inside a python interpreter. Or online if you wish to: http://www.datamech.com/devan/trypython/trypython.py
Try setting up a REPL (read-eval-print) environment using your favorite editor. For instance, emacs has good support for doing such tasks.

Categories

Resources