Python Shell - Syntax Error - python

I am very very new to Python. I am trying to get a python program to run by writing 'python hello.py' but every time I do that I get a Syntax Error. Why is that?
However when I open the file and click on run module, it works. Why won't it work if I type 'python hello.py'
Is there anyway I can navigate to the directory where the file is stored and then run it? I tried placing my file in directly in the Python23 folder :P didnt work anyway
Please help me. I have Python 2.3.5

I'm guessing that you're running python hello.py within the Python REPL. This won't work; python hello.py is something that starts Python that you'll need to run in your system shell.

What is the error?
Place python's filepath to the python.exe in your system's PATH, then you can run a python file from anywhere.

Related

Can't run Python scripts in Sublime on Mac OS

Just to get it out of the way, I'm new at Python and coding in general, but very eager to learn how to do it.
The problem is that I can't run Python scripts through Sublime Text. I'm using Mac OS 10.15.7, and I've downloaded the latest version of Python from the official website and then also with Anaconda.
When I try to run any Python script in Sublime, I get the following message:
/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: can't find '__main__' module in ''
[Finished in 0.3s with exit code 1]
[shell_cmd: python -u ""]
[dir: /private/var/folders/wh/lt9wn7qn7zl4p8xkj67bhpyh0000gn/T/AppTranslocation/86FABCDF-E2DC-4C15-A564-EFE9529B919B/d/Sublime Text.app/Contents/MacOS]
[path: /Users/alexherranz/opt/anaconda3/bin:/Users/alexherranz/opt/anaconda3/condabin:/Library/Frameworks/Python.framework/Versions/3.9/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin]
I looked at some other similar questions here, and I installed the SublimeREPL package as many recommended, but I'm probably missing something, as I still get this error message. It's quite frustrating, as I'd like to run the scripts within Sublime instead of copying it everytime to the terminal.
Is there a fix for the problem I'm running in to? Any ideas appreciated!
You've entered code in the editor, but you haven't saved it as a .py file. Save the file, hit ⌘B, and it should run just fine.
Make sure you save the file first, before attempting to run it.
If you "save as" the file somewhere on your computer you should be able to then go into the mac version of the file explorer and then double click your file. If it is a file that opens a window, like tkinter or turtle it will run the code normally. If not it should run the code in the mac version of the command prompt. Make sure it is opening the file with python.

Cannot get Python files to open in cmd

I am new to programming, only two weeks in, and I am using Python 3.8. I am working my way through "Automate the Boring Stuff." One of the projects has us opening files in the cmd line and I cannot get .py files to open using cmd. When I only type python it gives me good info:It tells me this is Python 3.8.5 yadda yadda yadda. Then, if I try to type python hello.py it says 'invalid syntax.' Wait there is more. So I close cmd and reopen, then type: python hello.py and it says 'can't open file 'hello.py' [Errno 2] no such file or directory.' This is maddening. Any help is appreciated.
So, invalid syntax means there is a problem with your code. It opened the file just fine the first time you did it. If you want help with this, you'll need to post your code.
The second problem is probably because the file is in a specific folder, and you weren't in it. For example:
- ~/ #The command line starts here
- documents/
- programming/
- hello.py >This is where your file is
If you try to run python hello.py while in the home folder, it won't work because hello.py isn't in that folder. You have to go into the folder where it is.
Once you know which folders it's in (you can see a list of folders with ls on mac and dir on windows), you can go into that folder with cd folder_name. When you can type ls or dir and it one of the items is hello.py, then run python hello.py
Please note that the folder system I showed above is a guess - I have absolutely no idea where your file is.
When you type "python" in the command line, you enter the Python Interactive Shell. This means that you are able to type python code and execute it. You know you are in the Python Interactive Shell when you see >>> at the start of the line. It is saying "invalid syntax" because you are trying to run python hello.py in the Python Interactive Shell. You can type quit() and hit enter to exit the shell.
Your second issue is happening because you don't specify the directory in which the python file is located. For example, let's say your python script, hello.py, is saved on your Desktop. You need to utilize the cd command, which is short for change directory.
cd Desktop
will change your current working directory to the Desktop. If your file is located in the Desktop, you can then type
python hello.py
and your script will run.

running a python script through powershell

this may seem basic, but could somebody run me through how to run a python file (one that's already created), through powershell? I know absolutely nothing about powershell despite hours of looking online to learn
Thanks all
It is happily very similar, if not the same, as running a python script from the normal command line.
First you're going to need to have python installed and in your path.
To test this try python --version in powershell. You should get output like: python 2.7.
If that worked fine then you run your script by typing python followed by the script name i.e. python test.py (if its in another directory you will need to go to that dir or add the dir to the filename).
If that didn't work you probably need to install python: https://www.python.org/
Provide the path where you have installed the Python, followed by the path of the Python script:
'path of python.exe' 'Path of the python script'
Example:
C:\\Python27\python.exe 'D:\\Project\script.py'

Run python program from terminal

I have downloaded a python program from git.
This program is python 3.
On my laptop i have both python 2.7 and python 3.4. Python 2.7 is default version.
when i want run this program in terminal it gives some module errors because of it used the wrong version.
how can i force an name.py file to open in an (non) default version of python.
I have tried so search on google but this without any result because of lack of search tags.
also just trying things like ./name.py python3 but with same result(error)
When you type "python", your path is searched to run this version. But, if you specify the absolute path of the other python, you run it the way you want it.
Here, in my laptop, I have /home/user/python3_4 and /home/user/python2_7. If I type python, the 3.4 version is executed, because this directory is set in my path variable. When I want to test some scripts from the 2.7 version, I type in the command line: /home/user/python2_7/bin/python script.py. (Both directory were chosen by me. It's not the default for python, of course).
I hope it can help you.
Use the shebang #!<path_to_python_version_you_want>
As in:
#!/usr/bin/env python
at the very top of your .py file
Also checkout: Should I put #! (shebang) in Python scripts, and what form should it take?
The Method of #Tom Dalton and #n1c9 work for me!
python3 name.py

Trying use a .pth file to add a path in documents folder on Mac

Hi everyone I'm trying to get Python configured on an OS X laptop and I'm having some trouble. I'm both new to Python and am very unfamiliar with the UNIX terminal. What I'd like to be able to do is to have a directory in my documents folder that would contain python modules and be able to run them from the command line. Currently I have a Python Directory and a chaos.py module inside of it. The full path is /Users/Ben/Documents/Python/chaos.py.
So I followed the steps here and here. I can see that the site-packages for Python 3.4 is in a few spots but I chose this one: '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages' to place the .pth file.
I created a file named Ben.pth in this location with the contents: /Users/Ben/Documents/Python/
Now from my (very limited) understanding that should be all I would need to do for Python to start looking right? So I try to run python3 chaos.py in terminal and I get an error:
/Library/Frameworks/Python.framework/Versions/3.4/Resources/Python.app/Contents/MacOS/Python: can't open file 'chaos.py': [Errno 2] No such file or directory
I'll also try opening IDLE clicking File->Open Module... and try to open it from there and I'll recieve a "module not found" box.
I'm completely stumped, I'm not sure if its a syntax error that I made somewhere (again I don't really know what I'm doing with the UNIX commands) or if I'm just way out in right field. If anyone could help me out, I would greatly appreciate it! Thanks!
Forget the .pth stuff for now, that's not something you'd normally do. In a unix-ish environment, the typical way you'd run a script would be to change directory:
cd /Users/Ben/Documents/Python/
and then run the script:
python chaos.py
Another way to do it would be to run the script with an absolute path; you can do this no matter your current working directory:
python /Users/Ben/Documents/Python/chaos.py
Finally, if you've written a utility script you want to be run from anywhere without typing that absolute path all the time, you can do something a little fancier...
Add a 'shebang' line as the first line of your script. It'll go like this:
#!/usr/bin/env python
Get into the directory where your script lives:
cd /Users/Ben/Documents/Python/
Make the script executable:
chmod +x chaos.py
Put a link to the script in a directory on your path... /usr/local/bin/ could be a good choice:
ln -s /Users/Ben/Documents/Python/chaos.py /usr/local/bin/chaos.py
Now you can type chaos.py anywhere on your system and it'll run.

Categories

Resources