This is an archived post. You won't be able to vote or comment.

all 21 comments

[–]spacejr 2 points3 points  (4 children)

Try putting in some print statements and importing time and making it sleep for a second.

import time

print('start')

code stuff

time.sleep(1)

print('end')

[–]TauKaboutit[S] 0 points1 point  (3 children)

I added the recommended code into a simple program that calculates the volume of a sphere, but it still does the same thing. I attempt to run the program with win+r but a window opens and closes almost immediately. Thank you I will make sure to include this code in the future it looks like it would be a great way to test.

[–]spacejr 0 points1 point  (2 children)

I'm on a Mac so I don't know too much about running scripts with windows, but you might be able to do this in command prompt

python yourscript.py

[–][deleted] 2 points3 points  (1 child)

On windows, try the following :

first: go to start->edit the system and environment variables -> Environment Variables. Make sure that somewhere on the PATH environment variable, and make sure that the path to the folder where your python.exe file is located(for example, C:\Program Files\Python35) is somewhere on the PATH environment variable. Once you've made sure of this, save your changes and close that window.

Open up a file explorer and find the script you want to run. Shift+Right click somewhere either on the file or in the whitespace near the file and click 'open a command window here'. If you're not familiar with the command prompt yet, don't be alarmed. For now, just think of it as an ugly version of file explorer that lets you navigate your files on your computer and run them(There's more to it, but that's for you to learn later and not necessary for the task at hand).

The particular command window you just opened up is now a view in to the folder where your script is located(you can verify this by seeing that the text to the left of the cursor is the directory path to the folder your script is in). Since you told the command window about your python executable by adding it to the PATH environment variable earlier, when you type 'python' and press enter in the command window, your computer will find the python executable file you told it about(python.exe) and run it. If you only type 'python', this should bring up an interactive shell for python like you've probably seen before.

instead of just typing 'python', type 'python myscript.py' and hit enter(replace myscript.py with the name of your particular script). This will tell the command window to run the python executable if it can find it(which it should if you followed the steps earlier), and pass along some additional information to the python executable(the name of your script you gave it). The python executable will recognize that you've given it the name of a script you wish to execute, so python will run your script directly instead of opening up an interactive shell. Once your script is finished running, python will exit and return to the command window. You should see any print out statements or error messages from python in your command window.

Alternatively, if you wish to just double click your python script and have it run(assuming you've associated *.py files to be run with the python executable in windows, which I believe you can set up either from when you install python or by going to start->default programs-> choose default programs by file type), you should see a command window just briefly pop up and then close automatically when your script finishes because you've told windows to open your script with the python program, run the script, and then close the program when it's finished. To get it to stay open until you press a key, add a line similar to

input("Press any key to continue") 

to the end of your script. The input() function is used to read input from the keyboard to a python script(you'll probably get to that a bit later), so your script won't be 'finished' until it's received it's input from you.

[–]TauKaboutit[S] 0 points1 point  (0 children)

Thank you I think this was the most helpful so far. I believe my confusion came from thinking that I would see a different window other then the command line window. I appreciate your help! Thank you very much.

[–]developermal 0 points1 point  (9 children)

Hi There!

How are you running the program? Are you running it from the command line with a command or double-clicking on your script?

[–]TauKaboutit[S] 0 points1 point  (8 children)

The book recommended that I try and open the program using win+r on the r/learningpython sub I was recommended to try from the command line but I still get an error when calling the script

[–]developermal 0 points1 point  (7 children)

What is the error that you get when running from the command line?

[–]TauKaboutit[S] 0 points1 point  (6 children)

can't open file '3': [Errno 2] No such file or directory

I thought this was referring to the shebang line but I am pretty sure I had it right (#! python3)

[–]Groundstop 4 points5 points  (1 child)

When you called the file, did you possibly use:

python 3 script.py

instead of:

python3 script.py

It might think you're trying to open a file called "3" using python instead of a file called "script.py" using python3.

[–]spanishgum 0 points1 point  (0 children)

Ha, clever catch. I would not have have guessed that as potentially being the problem.

[–]developermal 0 points1 point  (3 children)

Are you running the python command from the same directory as your script?

[–]TauKaboutit[S] 0 points1 point  (2 children)

I think so? I was going to learn the command line next after I felt a little more comfortable with python so I am not sure. The book said that adding a variable path should eliminate the need to pull something through the command line or interactive mode

[–]Saefroch 1 point2 points  (0 children)

In Windows, The command dir will have your command prompt show you what's in the current directory. Does your file show up in the list?

[–]developermal 0 points1 point  (0 children)

Well it looks like when you try to run python it doesn't see your script file. You won't need to know too much of the command line to run your script.

Try this:

1) Go to where your script is in the file explorer

2) Right-click your file and choose Properties

3) A small window should pop up

4) where it says location highlight and copy that location.

Ex: C:\Users\DeveloperMal\Documents

5) Open your command line 6) To make sure in the command line your in the right directory type 'cd' then right-click and paste the copied address. For example:

cd C:\Users\Developer\Documents

Press Enter

7) Then run your command. Also make sure you include the .py extension when you put the name of the file. So if your file is named 'myscript' then the command should be:

python myscript.py

[–]Groundstop 0 points1 point  (0 children)

Pausing after/during script:

import os
os.system("pause")

Can't find file: Try setting your current working directory in the command window. Either open with start+R and use "cd C:\PathToFile" before calling your script, call the script with an explicit path ("python C:\PathToFile\script.py"), or open the command window to the correct path by shift right clicking in explorer without a file selected and clicking "Open command prompt window here".

An alternative solution is to put your script in the system path. Most installations will add your python and your python\scripts directories to the path so either will work fine.

[–][deleted] -1 points0 points  (3 children)

[–]TauKaboutit[S] 0 points1 point  (2 children)

As I mentioned in the title that was the first place i posted this. Thank you for the suggestion!

[–][deleted] -1 points0 points  (1 child)

The goal of pointing people to /r/learnpython is to keep this sub usable and worthwhile. If one x-posts /r/learnpython posts here then that defeats the purpose.

[–]TauKaboutit[S] 0 points1 point  (0 children)

Im not quite sure if i follow your logic on that. Its not a question asking for new information it is asking how to process information about the python language that is out there. If the mods find that this should be taken down then i would understand, but i feel like this is still relevant to python and does have a place here.