Related learning recommendations:linux video tutorial
How many are available under linux There are three ways to run python:
1. Command line execution:
Create a *.py document and write python code in it. After that, execute on the command line:
$ python *.py
Notes: 1. The absolute path of the file needs to be specified; 2. The output must be specified in the source code, such as print
2. UNIX script :
After creating the *.py file, you need to add the python execution path to the head of the source code, such as "#!/usr/bin/python". Among them, "#!" is called "hash bang". After that, first change the text permission to the executable file, and then execute it:
$ chmod +x *.py $ ./*.py
Notes:1. If the python execution path cannot be found, enter $ which python to view; 2. Another more applicable method is to add an env program to automatically find the python path, such as rewriting the first line to "#!/usr/bin/env python". This method facilitates cross-platform execution of the program, but the prerequisite is to ensure the env path; if you do not know the env path, enter $ which env to view.
3. Module loading and reloading:
Module (module): an encapsulation of a group of variable names. A module corresponds to this single "namespace". After loading the module, access the object through object.attribute. A module can be understood as a "package". The advantage of this is that all codes that deal with the same problem are packaged for repeated use. The module concept provided by python solves the problem of repeated variable names very well, because even if the variable names are the same, they can still be distinguished because the module names are different (much like the "double colon" operator in R language, package::function ).
Loading: import mymoduel or from mymodule import object; another method uses execfile('mymodule.py').
Overloading: Use the load() function, such as load(mymodule).
Notes: 1. Import/from/load() does not need to specify the module suffix, which also implies that the module needs to be in the current running directory; 2. After import/from, you cannot import/from again. effect. Similarly, import/from (the second time) does not work after modifying the module. This needs to be loaded with load(). execfile() does not have this problem.
4. The most enjoyable way:
Configure the python running environment in emacs.
Related learning recommendations:python tutorial
The above is the detailed content of How to run python on linux?. For more information, please follow other related articles on the PHP Chinese website!