This is not necessarily a straightforward question. If you are already familiar
with running programs from the Windows command line then everything will seem
obvious; otherwise, you might need a little more guidance.
Unless you use some sort of integrated development environment, you will end up
typing Windows commands into what is variously referred to as a “DOS window”
or “Command prompt window”. Usually you can create such a window from your
Start menu; under Windows 7 the menu selection is . You should be able to recognize
when you have started such a window because you will see a Windows “command
prompt”, which usually looks like this:
The letter may be different, and there might be other things after it, so you
might just as easily see something like:
D:\YourName\Projects\Python>
depending on how your computer has been set up and what else you have recently
done with it. Once you have started such a window, you are well on the way to
running Python programs.
You need to realize that your Python scripts have to be processed by another
program called the Python interpreter. The interpreter reads your script,
compiles it into bytecodes, and then executes the bytecodes to run your
program. So, how do you arrange for the interpreter to handle your Python?
First, you need to make sure that your command window recognises the word
“python” as an instruction to start the interpreter. If you have opened a
command window, you should try entering the command python and hitting
return.:
C:\Users\YourName> python
You should then see something like:
Python 2.7.3 (default, Apr 10 2012, 22.71:26) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
You have started the interpreter in “interactive mode”. That means you can enter
Python statements or expressions interactively and have them executed or
evaluated while you wait. This is one of Python’s strongest features. Check it
by entering a few expressions of your choice and seeing the results:
>>> print "Hello"
Hello
>>> "Hello" * 3
'HelloHelloHello'
Many people use the interactive mode as a convenient yet highly programmable
calculator. When you want to end your interactive Python session, hold the Ctrl
key down while you enter a Z, then hit the “Enter” key to get back to your
Windows command prompt.
You may also find that you have a Start-menu entry such as that results in you
seeing the >>> prompt in a new window. If so, the window will disappear
after you enter the Ctrl-Z character; Windows is running a single “python”
command in the window, and closes it when you terminate the interpreter.
If the python command, instead of displaying the interpreter prompt >>>,
gives you a message like:
'python' is not recognized as an internal or external command, operable program or batch file.
or:
then you need to make sure that your computer knows where to find the Python
interpreter. To do this you will have to modify a setting called PATH, which is
a list of directories where Windows will look for programs.
You should arrange for Python’s installation directory to be added to the PATH
of every command window as it starts. If you installed Python fairly recently
then the command
will probably tell you where it is installed; the usual location is something
like C:\Python27. Otherwise you will be reduced to a search of your whole
disk … use or hit the Search
button and look for “python.exe”. Supposing you discover that Python is
installed in the C:\Python27 directory (the default at the time of writing),
you should make sure that entering the command
starts up the interpreter as above (and don’t forget you’ll need a “Ctrl-Z” and
an “Enter” to get out of it). Once you have verified the directory, you can
add it to the system path to make it easier to start Python by just running
the python command. This is currently an option in the installer as of
CPython 2.7.
More information about environment variables can be found on the
Using Python on Windows page.
Yes, .pyd files are dll’s, but there are a few differences. If you have a DLL
named foo.pyd, then it must have a function initfoo(). You can then
write Python “import foo”, and Python will search for foo.pyd (as well as
foo.py, foo.pyc) and if it finds it, will attempt to call initfoo() to
initialize it. You do not link your .exe with foo.lib, as that would cause
Windows to require the DLL to be present.
Note that the search path for foo.pyd is PYTHONPATH, not the same as the path
that Windows uses to search for foo.dll. Also, foo.pyd need not be present to
run your program, whereas if you linked your program with a dll, the dll is
required. Of course, foo.pyd is required if you want to say import foo. In
a DLL, linkage is declared in the source code with __declspec(dllexport).
In a .pyd, linkage is defined in a list of available functions.
The FAQ does not recommend using tabs, and the Python style guide, PEP 8,
recommends 4 spaces for distributed Python code; this is also the Emacs
python-mode default.
Under any editor, mixing tabs and spaces is a bad idea. MSVC is no different in
this respect, and is easily configured to use spaces: Take , and for file type “Default” set “Tab size” and “Indent
size” to 4, and select the “Insert spaces” radio button.
If you suspect mixed tabs and spaces are causing problems in leading whitespace,
run Python with the -t switch or run the tabnanny module to
check a directory tree in batch mode.