Forums

How to run a file?

I can't seem to figure out how to run a file in my dashboard>files

Open your main .py file by clicking the name in Dashboard | Files. The file will be opened in edit mode, and you should see a handy 'Save & Run' button at top right.

Probably better though is to open a Bash console, 'cd' to the directory, and then 'python main.py' (for Python 2.7) or 'python33 main.py' (for Python 3.3) or similar.

hth Jim

+1 to what Jim says, that should work fine.

And building further on what Jim says, an awesome new, quicker way to get the Bash console to open with the current set of files as the working directory from Dashboard|Files is the 'Open Bash console here' button at the top of every page there. See it described in the blog entry here, under 'Start Console here option'.

Thanks wayne461 - now why hadn't I noticed that???

It's new, we just added it yesterday!

Ah, that's reassuring...

A good addition!

I can't see any save and run button only save or save as. I have no idea what a bash console is. Help. I usually use idle on my mac but I'm trying to teach kids can anyone help? Thanks

If you hover over the dark button with double arrows to the right of the Save as button, it should say Save & Run as a tool tip? That will trigger another window to open and you'll be in a python interactive console with your script running. If you are not seeing that, one question I asked is whether your file ends in '.py'?

And a bash console is just the equivalent of what you'd most likely see on your Mac if you opened the program Terminal. You can find that program typically under Other in the Mac Launchpad. It is a command line interface (a.k.a. 'shell') for interacting with your computer as opposed to the graphical user interface called 'Finder' you normally use on your Mac.

In PythonAnywhere, you trigger such an equivalent terminal to open from the Consoles tab at the site. Look for Other and to the right of that, click on Bash. Bash is the HINT: If you are looking to run a script from inside that terminal, you may need to change directories ,a.k.a. folders, to be where your script is. It would involve navigating using the cd command.

Thanks for your reply. I hit save and run but the page just seemed to refresh it didn't open a new window running the code. The file ends in .py and it runs fine in IDLE on my mac. Not sure what to do now. Amy

Ok solved the problem. I had pop ups blocked! :-D Thanks for your help Amy

I almost had written a warning that I didn't know how pop-up blockers might feel about that new window that would be spawned.

Glad it worked out. I was introduced to Python via IDLE as well, and I had once been lost for a bit too when I had to figure out how run Python without IDLE when I moved from a Windows PC to a Mac. Running scripts caused IDLE on that Mac to crash for some reason and troubleshooting was a nightmare since I didn't have admin privileges on the machine. This was before the wonderful PythonAnywhere came along.

Thank you Wayne461

How do I run a file. I have allowed pop ups and don't see Save & run。

Andrew, they just updated the style of the site recently.
With the script file open in my browser, the button I see now in the upper right corner is >>>Run and when I hover over it, it says "Save & Run (Ctrl + R)."

Thanks for the help wayne461.

I am using Chrome and IE. None of them is showing the Run button. I tried the shortcut Ctrl+R, it didn't not work for me either. I tried the code on my local machine and pythontutor.com, both worked.

I have just tried it again. It seems the Ctrl+R shortcut worked but I still don't see that Run button.

hmm- what does the top of your screen look like? / what do you see there / can you take a screenshot?

Also, what's your file called? Does the filename end with ".py"?

I too do not see the run or save and run button. CTRL + R does work, after I closed the other consoles.

No buttons to be seen

Does the filename of your Python file end with ".py"? If it doesn't, you won't have a run button.

Sounds like the console limit is the issue with CTRL + R. I think the button and CTRL+R shortcuts need to spawn consoles and if you are at your limit, I assume it is a problem? Anyone know if the button option is affected by number of consoles already running?
Just to point out something for novices trying to get started here. Now buried at the top of this thread is the second part of jgmdavies original response to the original question. That path to running a Python script at PythonAnywhere he describes is probably what you want to learn for going forward anyways. (Plus that approach is nearly universal as it works on any command line where you can run Python.) I will reiterate it here, assuming your full script file name ends in .py:
You navigate using the Bash console to the directory with your script and then type python <SCRIPT_NAME_HERE> for Python 2.7 or python3.5 <SCRIPT_NAME_HERE> (for Python 3.5). (Presently in my Bash consoles python3.6 doesn't work but my Pythonanywhere account is rather old and your mileage may vary, as described here.)

It was definitely a .py extension problem.

:)

It is indeed a bit strange the default extension for a file is not .py for a site dedicated on Python ;-)

it's a feature not a bug! :D

note that there should still be a "start a console at this location" button even if it's not a .py file.

We don't have a "run this file in python" button because it could also just as easily be say a bash script, or a json file etc- it would have syntax errors/really weird effects if we included a "run in python" button for these files.

This is working as Jim described using the python command. But I can't get it to work trying to use the later versions of Python. I can open a console in Python 3.5 or Python 3.6. But commands such as python35 or python36 do not work. Do I have to define these commands before I can use them?

Did you try python3.5 <SCRIPT_NAME_HERE> or python3.6 <SCRIPT_NAME_HERE> ? Like I said in my December 13th post, the specific Python 3 varieties that work can vary with the age of your account.

That works. I didn't think I could have a period in a command. I missed your Dec 13 post until you pointed it out. Thanks!

i am making a file on python 3.6 but there is no save and run button. What do i do?

Name it with a .py extension.

i want to use python 3.7 to open i file. when i click on run file it run with python 2.7 any suggestion

Does it have a hashbang at the start? That is, is the first line something like

1
#!/usr/bin/python

...?

If so, try changing it to this:

1
#!/usr/bin/python3.7

hi i want to run a code with python 3.7 in console bash. How i can do it,,, by default is python 2.7

You can use the python3.7 command instead of just python -- that is, run

python3.7 myscript.py

instead of

python myscript.py