+ 1
How do i make a python file executable from the console?
I want to make my file executable from the console example: when it shows me ">>>" I can type ">>>mine_pet" And it will proceed with the code in the mine_pet file. if you need the code I'm using I can comment it down below. also, if this is not possible, please let me know. if there are easier ways to go about this that endup with the same result please put both ways if you can. thank you in advance. sorry if this confuses you, I can clarify what I mean if I need to.
17 Answers
+ 1
I know very little about Python, but this should help you out. ^-^
http://pythoncentral.io/execute-python-script-file-shell/
+ 8
@Supersebi3 using 'import test' is a lot simpler than 'exec(open("test.py").read()' So far the only difference I can see is that 'import' only works once in a code (but presumably works as often as you use it in the console).
# import vs exec() experiment
with open("test.py", "w") as newfile:
newfile.write("print('hello world!')")
print("first import")
import test
print ("second import")
import test
print("first exec")
exec(open("test.py").read())
print("second exec")
exec(open("test.py").read())
''' output:
first import
hello world!
second import
first exec
hello world!
second exec
hello world!
'''
+ 7
Thanks for info @Supersebi3 @David Ashton,
+ 6
@MrCoder execfile() causes an error. It works in Python 2 but was removed in Python 3.
"Removed execfile(). Instead of execfile(fn) use exec(open(fn).read())."
from https://docs.python.org/3.3/whatsnew/3.0.html?highlight=execfile#builtins
+ 6
@Arick path is the way you navigate to the folder your file is in. See https://www.computerhope.com/jargon/p/path.htm
+ 5
If you save your python file (e.g. "your_file.py") in the same directory as python.exe, you can run it from the console with the command:
>>>exec(open("your_file.py").read())
You can also write your file there with (e.g.)
>>>newfile = open("your_file.py", "w")
>>>newfile.write("print('hello world!')")
>>>newfile.close()
You can check to make sure it is there with
>>>import os
>>>os.listdir()
You should get something like this:
['DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt', 'python.exe', 'python3.dll', 'python36.dll', 'pythonw.exe', 'Scripts', 'tcl', 'Tools', 'vcruntime140.dll', 'your_file.py']
This is what you get when you run it:
>>>exec(open("your_file.py").read())
hello world!
+ 5
@Arick now I see why you asked about 'path'. @Supersebi3 was 'open(filepath)' a typo? 🙂
+ 4
execfile("filename.py")
+ 2
The function execfile() doesnt exist in Python 3. Instead you have to do exec(open(filepath).read()). You dont need more explanation unless youre going to work with python 2
+ 2
ok, file path is the name of the file correct?
+ 2
@David Ashton, no, you can do
open("./some/path/to/your/file.py")
you cant do that with imports afaik
+ 2
ok!!! thank you for the help!!! this has helped me tremendously!
+ 1
import filename_without_.py if its in the same directory as youre in
+ 1
as I am new to python, I have only gotten to work with Python 3.6. would it be possible for you to explain this in detail?
0
ok, I will try it, thank you for the quick reply
0
the page has successfully confused me
0
can anybody give me the lines of code to run with explanation? I have Python so I don't need the tutorial on getting an interpreter from the console, I'm using Windows if that changes anything