Python Interactive Session

Published May 5, 2020, 1:31 p.m.

What is the point of python interactive sessions?  Firstly, the interactive session helps you to get comfortable with using python in the shell environment.  Secondly it is a great testbed for debugging code.  You can test out an idea and see if it will raise an error or not.  You can attempt to import a package to see if you have it already or not.  Basically, the interactive session will make python feel more comfortable and help you tease out some ideas to find out which ones might work and which ones won't. 

One thing to note about the interactive session is that your program is not preserved.  Single lines are preserved in the sense that a log of your commands are stored and you can retrieve those commands by pressing the up arrow key.  However, the point of the interactive session is to test some idea "on-the-fly" and then, once you know it works, capture it in a program.

First off lets set some ground rules.  Let us make a space for our python projects.  I don't care what you call it or where you make it.  I will assume that you make it on desktop and that you call it PythonWork.  The only reason that this is important is that eventually we will start making scripts or programs that we want to run and it will be nice if they are all located in a similar place.

If you are running Anaconda then I would suggest using the windows menu to run either Anaconda Navigator or Anaconda Prompt.  This is because the Anaconda version of python is often installed on a per-user basis in a local user folder.  Running them through the Navigator or Prompt will be sure to "activate" python in the terminal.  Incidentally, this location might be a good one to store your projects.

Once you have a folder for your python projects it might be nice to have a shortcut to open a terminal window.

Navigate to the folder where you want the shortcut. 

Then to create a cmd shortcut right click and select "New" and  "Shortcut".

copy the following:

%windir%\system32\cmd.exe

as the "location of the item" and click Next. 

Then give it a name like "cmd in myfolder". 

Finally, right click the shortcut and edit the field "Start In" to match the path to your folder. (Ex. C:\Users\Username\Desktop\PythonWork\)

Now when you double click your shortcut you have a terminal window opened up in the folder where you are going to put your python files!

Whichever way you use to open a terminal, once it is open you should simply be able to type "python" to start an interactive session.

C:\PythonProject>python
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

All versions of python come with some pre-installed packages.  Some of the packages that are used frequently are called 'sys' and 'os'.  In python we can import them for use with the command:

C:\PythonProject>python
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> import os

If we want to find out what we can do with these newly imported modules, we can simply type "help(sys)" and hit return which yeilds:

>>>help(sys)
Help on built-in module sys:

NAME
    sys

MODULE REFERENCE
    https://docs.python.org/3.8/library/sys

    The following documentation is automatically generated from the Python
    source files.  It may be incomplete, incorrect or include features that
    are considered implementation detail and may vary between Python
    implementations.  When in doubt, consult the module reference at the
    location listed above.

DESCRIPTION
    This module provides access to some objects used or maintained by the
    interpreter and to functions that interact strongly with the interpreter.

    Dynamic objects:

    argv -- command line arguments; argv[0] is the script pathname if known
    path -- module search path; path[0] is the script directory, else ''
    modules -- dictionary of loaded modules
-- More  --

Notice that where it says "-- More --" if you continue to hit the enter button, more lines of "help" text will appear until it is exhausted.  Also notice that at near the top there is a Module Reference:

MODULE REFERENCE
    https://docs.python.org/3.8/library/sys

That url = https://docs.python.org/3.8/library/sys takes you to the documentation on the particular module. 

Getting to know these modules comes with time, experience and exposure.  Rest assured that if there is some functionality that you want out of a program that others have probably wanted it and figure out how to make it happen.  In the next tutorial we are going to play around with the interactive session and go over several standard variable types in python.

skip_nextPython Data Types
  • Download Python!

  • Python Interactive Session
    (currently viewing)
  • Python Data Types

  • Python Containers

  • Loops and Logic