CMD won't display my .py file [duplicate] - python

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Can’t run Python from cmd line
I've decided on learning Python, and I'm running Windows 7 Starter Edition on an Acer Aspire One netbook. I installed Python 2.7 directly under the C:/ directory.
I've added it to the PATH. When I type Python in the CMD, I get the appropriate message, talking about my version.
I made a file via Programmer's Notepad containing the following:
print "Hello world"
That is all I typed, and saved it as hello.py on my desktop.
I opened up CMD, and typed hello.py expecting to have Hello World pop up, but there is just a blank line, then prompts a command again.
Here is exactly what happens.
C:\Users\Shane>hello.py
C:\Users\Shane>
Please help. Thank you.

1) You saved your hello.py to your Desktop, but then you are running it from your home directory. That should actually produce an error
2) Try running it like this from your desktop: python Desktop\hello.py

Related

How do force or change my terminal to run python executable file in python 3 just by opening the file?

My terminal is running python 2 so when I run my python file the program fails.
So basically I am new to programming and trying to create a small python script to help me auto create folders. I also want to give the script to colleges so that they can use it on their systems too.
I know that I can run my file in terminal by using "python3 myfile.py" and it will work, but that's too much off a mission to do for my colleges and as my colleges are not familiar with code or terminal for that matter, I wanted to create an executable file so that they just click to open type a few answers to the promoted question and boom folders created.
This is where I run into a problem, I have "#!/usr/bin/env python3" at the top of my file but when I run the script IDLE opens up and it just shows the code I have written but doesn't seem to run the actual script I wrote. Am I doing something wrong?
I also then though perhaps I could just use the terminal to run the file as it is now executable, so I go into terminal and enter "myfile.py" and the program runs but in python 2 so my script fails as it is in python3. So another question would be is there a way to code into my python file, when running this file make sure you use python3? as I would want this to work on all colleges system without them having to write out anything in terminal?
Sorry for the long explanation but any advice would be greatly appreciated.
Thank you in advance
When you are on windows you can just create a .bat file where you write: python3 myfile.py in it.
Both files have to be in the same directory.
If you want to have a .exe you can also use py2exe.
You could also try just #!/usr/bin/python3 without env.
More here.

how to run python command line [duplicate]

This question already has answers here:
Running a python script from the command line in Windows
(3 answers)
Closed 6 years ago.
I have pycharm installed and do my class assignment in it and things work well. The teacher wants on to now run some of the programs in command prompt. Given that my programs and the path is here: "C:\Python27\python.exe C:/Users/sz5062/PycharmProjects/untitled1/main.py", how can I run a program that is saved the above path in command line? So, example, how would I run the main.py in command line, not shell prompt?
I have searched and searched for answers here, but I can't fine one. Thanks very much for your help.
This is the full instruction: Install Python and a programming text editor and write a program that prints one line other than 'hello world', then take two screen shots and upload them below. You should use the command line to execute the Python program you wrote in the text editor. Please do not use the IDLE Python Shell, the Python Interpreter (>>>), or a shortcut in your text editor to run the code.
When i run "python main.py", I get an error message "python is not recognized as inernal or external command".
On you Commandline enter:
C:\Python27\python.exe C:\Users\sz5062\PycharmProjects\untitled1\main.py
The above command should work from every directory.
First part is the program you start (python.exe). Second part is the argument you pass to the program(C:\Users\sz5062\PycharmProjects\untitled1\main.py). This is the path to your python code so that python.exe knows what you want to execute.
If you add C:\Python27\ to your PATH variable, you can navigate to your directory C:\Users\sz5062\PycharmProjects\untitled1\ and start your programm with:
python main.py
I'd recommend making sure that python is on your PATH (if you type python does it open a shell?) and then navigate to the C:/Users/sz5062/PycharmProjects/untitled1 directory. Once there you can do:
python main.py
and that should work.
I think I made this hard. since python was in installed on C:\python27, all I needed to run the command line was simply to write the code in a text editor, save it in the same directory and call it from there. So, essentially, c:\python27\code.py

Problems running Python in Notepad++ [duplicate]

This question already has answers here:
How do you run a python script from within notepad++? [duplicate]
(6 answers)
Closed 7 years ago.
I'm having trouble executing my programs in Notepad++. I'm currently operating on Windows 7.
When attempting to run the program in the the interpreter, I have to jump through numerous hoops to actually get my program to execute. In Notepad++, I'm unable to provide additional text if I'd like to run a sys.argv command, or need to write in a name for a function.
Are there any solutions? Any easier way to run my code?
Run your code from command interpreter.It will look like this
C:\Python27\python.exe programe_name.py
change directory where your program is save before the command.I recommend you to download IDLE editor.It is easy way to learn python.
What I have been doing is this:
Press F5 to run.
Paste this | C:\Python27\python.exe "$(FULL_CURRENT_PATH)"
Replace Python27 with whatever version you use.
Then run.
It should take what you're writing and run it, make sure to select Python as the current language too.
Sorry if this isn't what you're looking for, I'm just starting too!
There are better editors which can run python without a problem. Github's atom can do this easily and is a great free solution and Sublime Text 2 is also both free and paid.
However, if you absolutely have to run on NotePad++, do click Run on the Menu and then Run again. Now, type in the shell command you would like to run (Path-to-python.exe + Path-To-File) and click run

How to get a .PYC to not close after getting an error [duplicate]

This question already has answers here:
How to keep a Python script output window open?
(27 answers)
Closed 9 years ago.
I have a .PYC script I sent to someone, and it's crashing and giving an error that i'm not getting on my end. Is there anyway I can get the window to not close when it crashes so I can read the error message?
Run the script from an already-open terminal. To do this, open a terminal/command prompt and type:
python script.pyc
For this to work, you need to have the python executable in your path. Google "How to edit environment variables on [Your OS]" and add C:\PYTHON33. (Or whatever the directory is that you have installed Python to.)
When the error message appears, the terminal window will stay open for you.

Display Python Output in Sublime text [duplicate]

This question already has answers here:
How do I run Python code from Sublime Text 2?
(16 answers)
Closed 7 years ago.
New to Python & Sublime
Problem:
I type 'print ("Hello world")
How do I get it to show me the output (Hello world), is it in a separate window? or...
I understand I can use the Python Console built in, but that's a command line, what about when I get to use a ton of code, how do I get the output?
Click on tools > build systems > select Python, then Build with using ctr+shif B and select python, and it ll work. Second time you can use the build command, ctr + B since python is now set as default
My Sublime Extras extension, and MiniPy, the extension I stole it from, both allow this. Search "eval" on the Extras Github page or use MiniPy.
Maybe SublimeREPL, too, depending in what you are after.
Press CtrlB
Alternatively, if you're on linux or mac, save the file as hello.py in your home directory and do the following in a terminal / command line:
$ cat hello.py
print("Hello World")
$ python3 hello.py
Hello World
If you're on Windows you're best off using the built-in functionality of sublime.

Categories

Resources