![]() |
How to Execute python code - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: Tutorials (https://python-forum.io/forum-4.html) +---- Forum: Python Installation and Execution (https://python-forum.io/forum-41.html) +---- Thread: How to Execute python code (/thread-117.html) |
How to Execute python code - metulburr - Sep-01-2016 There are 3 main ways to execute python code. 1) Interactive Prompt
This is mostly used for experimenting and testing. Although you can write out long codes here, you should switch over to a file if you plan to write more than 3-5 lines. An IDE or file would be more appropriate and easier. The >>> is the prompt for interactive mode. What you type here is python code. Unlike the other methods of executing python, It will be executed right after you hit Enter, upon each line. The only time it does not execute after Enter (or each line) is when you are defining a function or class or a loop, for example. The ... will indicate that you are inside the block of what you are defining. **Some IDE's do not have the ..., in which it is advised to find a different IDE at that point. Once you finish writing the code inside the block, you must hit Enter twice, where it will execute and take you back to the >>> prompt depending on what you are defining. The reason the interactive prompt is for testing is because: 1) it will execute upon each line or after the definition of a loop, class, function, etc. 2) Once you exit the interactive prompt, what code you wrote in it, is gone. There is no saving the code. The other two methods of executing python code will save a file with your code in it. Interactive prompt help() while in the interactive prompt, you can use the help function to show the documentation for that specific module. For 3rd party modules you have installed you must import it and then help(your_module), it will show a description, classes, functions, version number, author. For builtins it will show class and methods and description of each method, etc. default help screenclass, methods, >>>help()import module and help() >>>from bs4 import BeautifulSoup >>>help(BeautifulSoup) >>>import urllib >>>help(urllib)show me the documentation for strings >>>help(str)show the specific method s.find() >>>help(str.find)show all modules installed for this version of python >>>help('modules') 2) IDE (Python script)
An IDE is just a fancy text editor. That's it. It does not perform magic tricks. You can accomplish the same without ever installing an IDE. See below for Command line / Terminal as this does just that. The bare bones of an IDE just creates a file and puts your text in that file, with a button to execute it. There are numerous IDE's. The only difference is preference. Some are free and some are not, but give out a free version that will work. IDE's also come with extra features such as a debugger and an embedded view of the program structure. Each IDE is different in how you configure it, execute code, etc. Some have a run command, and F# key, Ctrl+b, etc. to execute the python code. Some will open a terminal/console with for the output, some will have embedded terminals, and some will have their own area for output. Some will have all of these options, some will have only a few of these options. All IDE's can be re-configured to your liking.For example: If you don't like that F5 runs your code, you can change the keybinding to Ctrl + b, or vice-versa. If you want an embedded terminal in the IDE and the IDE doesn't do it by defualt, chances are there is a plugin for that IDE that will allow it. And so on and so on. What is IDLE? IDLE is just only one of the numerous IDE's. Just because python on windows comes with IDLE, does not mean python needs IDLE to run. It is there for beginner's convenience. Also by most programmers, it is also considered one of the worst IDE's too. It is one of the lowest quality environments for Python out there. It shall be fine for beginner's though. You can research why with a google search later, or run across the reasons yourself as you progess in programming. For those that stick with IDLE, IDLE starts you off with the python interpreter. This is not a command line! It is just a GUI form of the python interpreter that you can also get in the command line/terminal. It confuses a lot of beginner's because they think they click new window, write their code, and run their code in the python interpreter, thinking it is a command line or terminal. File -> New Window creates an empty file in the same way that Notepad does. And Run -> Run Module executes this code. The output of the code or errors (at least in IDLE) is displayed among the python interpreter, AKA ('>>>'). This is unique only to IDLE as other IDE's may not do this. And the python interpreter is there for a quick one liner tests here and there, not to run the code you saved in the new window. *IDLE is also one of the IDE's that does not show the ... in the embedded interpreter for indentation. IDLE also does not give some tracebacks for syntax errors. It is advised to find another IDE fast or use the command line, if you use the interpreter a lot. Screenshots *IDLE can interfere with programs using tkinter (or pygame) 3rd party libraries because of the IDE's own mainloop interfering with your programs mainloop. If you are confused on the process of using an IDE, or if you still think python requires idle to run, then install some other IDE's and use them for awhile. Try at least 5 of them out. In addition to that, open and write a file in notepad, and execute it in the command prompt. Then create a file, write in the file, and execute that file all in the command prompt all without using an IDE or GUI text editor. At this point you may start to understand the concept. 3) Command Line (Python script)
More often than you think, people will not use an IDE. In this case you open any text editor you want, write code, and when you save it you save it with the file extension of ".py". An IDE is just a fancy text editor, that's it! You can use anything, Gedit, notepad, IDLE, whatever to write the code. All IDE's have a way of quick run, but you can also run them from the terminal/DOS as shown below. test.pyFor Windows simplicity: put this test.py file in your directory that you started the interactive prompt: for example of the path: c:\Python32\test.pyand example of the same path executed: c:\Python32>python test.pyThen you open a Terminal/DOS prompt and change directory to your test.py and write this: python test.pyThe same as before like with the interactive prompt, but this time we add a argument and the argument is your .py file. So in short, you can either change to the directory that the file lives in and execute it from there: C:\>cd C:\my_python_lib C:\my_python_lib>python.exe test.pyor you can execute it from any directory if you give the full path of the file: C:\>python.exe C:\my_python_lib\test.pyDoes your program when executed just flicker and then go away? Most Windows users have got accustomed to double clicking an icon and have it execute. To adjust the program for double clicking, the basic method is to add the builtin input to have it ask the user for "nothing" to assure that the user must hit ENTER before the program closes out. raw_input() for Python 2.x and input() for Python 3.x. Just put this after your program at the end of the file and it will restrict the program to prematurely exiting. This is however only effective if you plan to execute the program by double clicking it. If you use a command line/Terminal to execute the program, you do not need the input function as the command line will still allow you to view the output of your program even after exiting, as you just get the users prompt when it exits. It depends on what OS you are on as to what the commands are. Some basics are: cd somethingto change directory to the the directory something (assumeing the file is named 'something'). Of course if that file was nested in another file, you first have to change to that directory (file), then to this one. ls to list the current directory contents in Linux dirto list the current directory contents in Windows Versions
How you execute the code also depends on the version. Code written for python2.x may not always execute without traceback errors in python3.x. So the syntax is NOT always the same, nor not always importing the same modules. If your not sure how to find your version, check here. The differences between the two versions can be found here. You may also have to change your IDE's settings to select a different python version from the default, if you have multiple python versions installed. UPDATE: Python 2.x will not be maintained after 2020. It is advised at this point to use python3.x unless you have a good reason to use 2.x. https://pythonclock.org/ Update 2: There was a significant change in possible syntax used in python3.6. Most noticeably for newcomers would be the f-strings or annotations. But you can always find the latest changes on python whats new section. What new for python 3.6. Such changes may require certain python versions to run, etc. For example if f-strings were in code, you could not use python3.5 or lower to run it. However python3.7 could, etc. Windows python install here
As of python 3.5, there is now a checkbox in the first installer page that asks if you want to add that python version to PATH. Most of the time, you are going to want that checked. Without python in the path, you can only run python in that one install directory, and not across the whole system. If you've already installed python without checking this box, you can of course do it manually Set Windows PythonPath Install Now installs python to the default directory (As of python3.5 is C:\Users\<USERNAME>\AppData\Local\Programs\Python\Python<VERSION>-<BITTYPE>). It also installs IDLE, pip, tkinter, python launcher, and the documentation. Python3.4 and under default install path is C:\Python34. Which is less keystrokes when moving there on the command prompt (which you will be using). Customize installation allows you to change the default install path. You can also opt out of installing pip, IDLE, tkinter, python launcher, etc. You may want to change the default install path as accessing AppData directory requires the ability to see [attachment=345][Image: attachment.php?aid=51] scripts do not use the shebang line in windows. In windows it is ignored. Windows however associates the .py extension with the python interpreter. This will allow you to double click the script to execute it with the associated interpreter. You can change this by right clicking on any py file. to run python from the command line, see the command line section of this same post. installing/Changing PATH for windows with screenshots https://www.howtogeek.com/197947/how-to-install-python-on-windows/
Open a Terminal. It does not matter what directory you are in. If you have both python2.x and python3.x installed, you can use either interactive prompt by a number. "python" is the default for your OS. The default may be python3.x in some distros (currently now is gentoo and arch) python2.x metulburr@ubuntu:~$ python Python 2.7.3 (default, Aug 1 2012, 05:14:39) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>python3.x metulburr@ubuntu:~$ python3 Python 3.2.3 (default, May 3 2012, 15:51:42) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>depending on your linux distro and time as more and more distros use python3.x as default in the comming years, python3.x may be default where python referes to 3.x and python2 referes to 2.x metulburr@ubuntu:~$ touch tester.py metulburr@ubuntu:~$ vim tester.py metulburr@ubuntu:~$ cat tester.py print('this is a test') metulburr@ubuntu:~$ python3 tester.py this is a test metulburr@ubuntu:~$This example shows the creation and execution of a one line python script.
#!/usr/bin/pythonand then make the script executable via chmod u+x scriptname.pyYou can then execute it via ./scriptname.py. At which point you can lose the .py to just do ./scriptname RE: How to Execute python code - micseydel - Oct-03-2016 This is sufficiently long, we should probably break it out into a pre-req and then stuff specific to each platform. RE: How to Execute python code - metulburr - Oct-03-2016 I better organized it as well as made some things collapsible. In doing so i realized that i dont really even have a good tutorial (other than setting windows python path) on windows execution. Although i am not quite fluent in it as i am never in windows anymore. |