2.1. Invoking the Interpreter¶
The Python interpreter is usually installed as /usr/local/bin/python3.9
on those machines where it is available; putting /usr/local/bin
in yourUnix shell’s search path makes it possible to start it by typing the command:
to the shell. 1 Since the choice of the directory where the interpreter livesis an installation option, other places are possible; check with your localPython guru or system administrator. (E.g., /usr/local/python
is apopular alternative location.)
You can learn more about Python support in Visual Studio Code in the documentation. In this release we addressed 42 issues, and it includes the ability to browse for or enter an interpreter path on selection. If you’re interested, you can check the full list of improvements in our changelog. Ability to browse for interpreter path. Python for Visual Studio Code is an extension for Visual Studio which aims at providing you with the best python development environment and experience possible. The extension is built for the Visual Studio Code IDE that is built from the ground up using Open Source technologies such as NodeJs. Python is an interpreted language, and in order to run Python code and get Python IntelliSense, you must tell VS Code which interpreter to use. From within VS Code, select a Python 3 interpreter by opening the Command Palette (Ctrl+Shift+P), start typing the Python: Select Interpreter command to search, then select the command. Python Tools for Visual Studio is a completely free extension, developed and supported by Microsoft with contributions from the community. Visit our Github page to see or participate in PTVS development. Visual Studio Community 2019 Free, fully-featured IDE for students, open-source and individual.
On Windows machines where you have installed Python from the Microsoft Store, the python3.9
command will be available. If you havethe py.exe launcher installed, you can use the py
command. See Excursus: Setting environment variables for other ways to launch Python.
Typing an end-of-file character (Control-D on Unix, Control-Z onWindows) at the primary prompt causes the interpreter to exit with a zero exitstatus. If that doesn’t work, you can exit the interpreter by typing thefollowing command: quit()
.
Visual Studio Code Python Interpreter Anaconda
The interpreter’s line-editing features include interactive editing, historysubstitution and code completion on systems that support the GNU Readline library.Perhaps the quickest check to see whether command line editing is supported istyping Control-P to the first Python prompt you get. If it beeps, youhave command line editing; see Appendix Interactive Input Editing and History Substitution for anintroduction to the keys. If nothing appears to happen, or if ^P
isechoed, command line editing isn’t available; you’ll only be able to usebackspace to remove characters from the current line.
The interpreter operates somewhat like the Unix shell: when called with standardinput connected to a tty device, it reads and executes commands interactively;when called with a file name argument or with a file as standard input, it readsand executes a script from that file.
A second way of starting the interpreter is python-ccommand[arg]...
,which executes the statement(s) in command, analogous to the shell’s-c
option. Since Python statements often contain spaces or othercharacters that are special to the shell, it is usually advised to quotecommand in its entirety with single quotes.
Some Python modules are also useful as scripts. These can be invoked usingpython-mmodule[arg]...
, which executes the source file for module asif you had spelled out its full name on the command line.
When a script file is used, it is sometimes useful to be able to run the scriptand enter interactive mode afterwards. This can be done by passing -i
before the script.
All command line options are described in Command line and environment.
Visual Studio Code Add Python Interpreter
2.1.1. Argument Passing¶
When known to the interpreter, the script name and additional argumentsthereafter are turned into a list of strings and assigned to the argv
variable in the sys
module. You can access this list by executing importsys
. The length of the list is at least one; when no script and no argumentsare given, sys.argv[0]
is an empty string. When the script name is given as'-'
(meaning standard input), sys.argv[0]
is set to '-'
. When-c
command is used, sys.argv[0]
is set to '-c'
. When-m
module is used, sys.argv[0]
is set to the full name of thelocated module. Options found after -c
command or -m
module are not consumed by the Python interpreter’s option processing butleft in sys.argv
for the command or module to handle.
2.1.2. Interactive Mode¶
Visual Studio Python Interpreter
When commands are read from a tty, the interpreter is said to be in interactivemode. In this mode it prompts for the next command with the primary prompt,usually three greater-than signs (>>>
); for continuation lines it promptswith the secondary prompt, by default three dots (...
). The interpreterprints a welcome message stating its version number and a copyright noticebefore printing the first prompt:
Continuation lines are needed when entering a multi-line construct. As anexample, take a look at this if
statement:
For more on interactive mode, see Interactive Mode.