+ 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.

21st Jul 2017, 10:55 PM
vortetty
vortetty - avatar
17 Respostas
+ 1
I know very little about Python, but this should help you out. ^-^ http://pythoncentral.io/execute-python-script-file-shell/
21st Jul 2017, 10:58 PM
Joshua Scalf
Joshua Scalf - avatar
+ 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! '''
24th Jul 2017, 1:19 AM
David Ashton
David Ashton - avatar
+ 7
Thanks for info @Supersebi3 @David Ashton,
24th Jul 2017, 12:11 AM
MrCoder
MrCoder - avatar
+ 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
22nd Jul 2017, 4:07 PM
David Ashton
David Ashton - avatar
+ 6
@Arick path is the way you navigate to the folder your file is in. See https://www.computerhope.com/jargon/p/path.htm
24th Jul 2017, 12:57 AM
David Ashton
David Ashton - avatar
+ 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!
22nd Jul 2017, 2:34 AM
David Ashton
David Ashton - avatar
+ 5
@Arick now I see why you asked about 'path'. @Supersebi3 was 'open(filepath)' a typo? 🙂
24th Jul 2017, 1:22 AM
David Ashton
David Ashton - avatar
+ 4
execfile("filename.py")
22nd Jul 2017, 8:40 AM
MrCoder
MrCoder - avatar
+ 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
23rd Jul 2017, 8:06 PM
Supersebi3
Supersebi3 - avatar
+ 2
ok, file path is the name of the file correct?
24th Jul 2017, 12:10 AM
vortetty
vortetty - avatar
+ 2
@David Ashton, no, you can do open("./some/path/to/your/file.py") you cant do that with imports afaik
24th Jul 2017, 8:15 AM
Supersebi3
Supersebi3 - avatar
+ 2
ok!!! thank you for the help!!! this has helped me tremendously!
24th Jul 2017, 2:44 PM
vortetty
vortetty - avatar
+ 1
import filename_without_.py if its in the same directory as youre in
22nd Jul 2017, 10:45 AM
Supersebi3
Supersebi3 - avatar
+ 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?
22nd Jul 2017, 11:08 PM
vortetty
vortetty - avatar
0
ok, I will try it, thank you for the quick reply
21st Jul 2017, 10:58 PM
vortetty
vortetty - avatar
0
the page has successfully confused me
21st Jul 2017, 11:02 PM
vortetty
vortetty - avatar
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
21st Jul 2017, 11:05 PM
vortetty
vortetty - avatar