Python: how to input python-code part like \input in Tex?
I want to input code in Python like \input{Sources/file.tex}
. How can I do it in Python?
[added]
Suppose I want to input data to: print("My data is here"+<input data here>)
.
Data
1, 3, 开发者_如何转开发5, 5, 6
The built-in execfile
function does what you ask, for example:
filename = "Sources/file.py"
execfile( filename )
This will execute the code from Sources/file.py
almost as if that code were embedded in the current file, and is thus very similar to #include
in C or \input
in LaTeX.
Note that execfile
also permits two optional arguments allowing you to specify the globals and locals dicts that the code should be executed with respect to, but in most cases this is not necessary. See pydoc execfile
for details.
There are occasional legitimate reasons to want to use execfile
. However, for the purpose of structuring large Python programs, it is conventional to separate your code into modules placed somewhere in the PYTHONPATH and to load them using the import
statement rather than executing them with execfile
. The advantages of import
over execfile
include:
- Imported functions get qualified with the name of the module, e.g.
module.myfunction
instead of justmyfunction
. - Your code doesn't need to hard-code where in the filesystem the file is located.
You can't do that in Python. You can import
objects from other modules.
otherfile.py:
def print_hello():
print "Hello World!"
main.py
import otherfile
otherfile.print_hello() # prints Hello World!
See the python tutorial
Say you have code in "my_file.py". Any line which is not in a method WILL get executed when you do:
import my_file
So for example if my_file.py has the following code in it:
print "hello"
Then in the interpreter you type:
import my_file
You will see "hello".
My question was clearly too broad, as the variety of replies hint -- none of them fully attack the question. The jchl targets the scenario where you get python-code to be executed. The THC4k addresses the situation where you want to use outside objects from modules. muckabout's reply is bad practice, as Xavier Ho mentioned, why on earth it uses import
when it could use exec
as well, the principle of least privileges to the dogs. One thing is still missing, probably because of the conflict between the term python-code
in the title and the addition of data
containing integers -- it is hard to claim that data is python-code
but the code explains how to input
data, evaluations
and executable
code.
#!/usr/bin/python
#
# Description: it works like the input -thing in Tex,
# you can fetch outside executable code, data or anything you like.
# Sorry I don't know precisely how input(things) works, maybe misusing terms
# or exaggerating.
#
# The reason why I wanted input -style thing is because I wanted to use more
# Python to write my lab-reports. Now, I don't need to mess data with
# executions and evalutions and data can be in clean files.
#TRIAL 1: Execution and Evaluation not from a file
executeMe="print('hello'); a = 'If you see me, it works'";
exec( executeMe )
print(a);
#TRIAL 2: printing file content
#
# and now with files
#
# $ cat IwillPrint007fromFile
# 007
f = open('./IwillPrint007fromFile', 'r');
msg = f.read()
print("If 007 == " + msg + " it works!");
# TRIAL 3: Evaluation from a file
#
# $cat IwillEvaluateSthing.py
# #!/usr/bin/python
# #
# # Description:
#
#
# evaluateMe = "If you see me again, you are breaking the rules of Sky."
f = open('./IwillEvaluateSthing.py', 'r');
exec(f.read());
print(evaluateMe);
精彩评论