Cannot get Python files to open in cmd - python

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.

Related

Can't run Python script from Terminal without changing directories even though directory is included in PATH

OS: Mac 10.14.6
Python Version: 3.8.5
New to Python and Bash so apologies if this is a dumb question but I can't find an answer anywhere. The closest I found was this answer on this thread however, I've already executed chmod +x on that file to change the permissions to allow it to be executable and I followed the instructions again and I still couldn't get it to work.
Basically I want to run Python scripts from a specified folder on my desktop (file path ~/Desktop/Python\ Scripts) through Terminal without having to change directories (out of pure laziness).
I added the folder to PATH and can see that it is listed when I run echo $PATH in Terminal. I thought that would do the trick but when I try to run the program with the command python boxprintV2.py as I usually would when I change directories I get python: can't open file 'boxprintV2.py': [Errno 2] No such file or directory
This command works fine if I change the current directory as I have been doing and I can run my program no problem but I would like to run from a new terminal window without having to change directory every time. Permissions on the file have been changed using chmod +x
Shebang from my program is #!/usr/bin/env python3.
If you run the command python <filename>, the Python interpreter will only check the current directory. Therefore, this only works if your working directory is "~/Desktop/Python Scripts", as you have already found out.
Because your script is marked as executable and it includes a shebang at the beginning of the file, you can just execute it directly from the command line by only entering boxprintV2.py. Bash will then search all directories in $PATH for this file and execute it.
Ok, I've found a workaround by creating a shell script following this answer on a different thread.
What I did was open a blank textedit file, go to format and convert it to plain text (or ⇧ + ⌘ + T which toggles rich text/plain text).
From there I typed these commands into the document as follows:
#! /bin/bash
cd ~/Desktop/Python\ Scripts
python boxprintV2.py
When I saved I didn't specify a file extension and unticked the box that said "If no extension is provided, use .txt". I'm not sure if this was necessary but I'm just detailing my exact workflow for anyone else who may have the same (laziness) problem as I do.
I then went back into a blank terminal window and entered:
chmod +x ~/Desktop/Python\ Scripts/boxprintV2to allow the shell script to be executed by all users.
From here I can just open the Python Scripts folder on my desktop, double click on the plain text file which is now a .exe and a new terminal window is opened with my Python script running!
It's literally going to save me tens of seconds of my life. I'm sure I'll waste them anyway.

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.

Opening Python and its files in Windows Powershell

I am currently reading the book Learn Python the Hard Way by Zed Shaw. On exercise 1 after learning the basics of Powershell we must open a notepad++ saved .py file in Powershell. Now here is the problem I'm having.
I am supposed to open this python file by running this command:
PS C:\Users\Trevor> python ex1.py
Zed Shaw does this in his book and it prints back what the file contains...("hello world")
Now i understand my path is wrong by the error message i receive telling me that python is not recognized. I have tried many many times to get the correct path to open python. I have saved the python27 file to my desktop and just about everything trying to get a path that will work.
I am starting at:
PS C:\Users\Trevor>
Any idea how to get to my python27 file and open python when it is saved to my desktop? I also have my ex1.py file saved to my python27 folder. Should i remove it? please help if you can thanks!
PowerShell cannot find python in the current directory or in the directories specified in PATH environment variable.
You can add your installed python directory to PATH variable in System Properties (Win+R → type in sysdm.cpl) → Advanced → Environment variables... → Under "user variables for ...", find PATH variable (if there aren't any, create it using New...), double click to edit it and insert "<your python path>;" (without the quotes). After that, restart PowerShell, run your command and you're done.
I also had this problem, but for me the solution to it wasn't creating a path. I typed in the same code as you, and this is the error message I got:
C:\Users\UserName\AppData\Local\Programs\Python\Python36\python.exe: can't open file 'ex1.py': [Errno 2] No such file or directory
The fix to this was a lot easier than I expected it to be, but it took me a little bit longer to figure out what it was since I just started learning how to work with Python and PowerShell.
This is what you need to do to solve this issue:
Instead of typing the name of the file you are trying to open, you first need to open the directory (the folder where all of your python files are stored).
To open the directory, type in cd DirectoryName and then press enter.
After that, type in "python" (without the quotes) and then the file name (for example, ex1.py). Then, press enter.
At this point, PowerShell should be able to open the file.
This is what you should see:
PS C:\Users\UserName> cd DirectoryName
PS C:\Users\UserName\DirectoryName> python FileName.py
(Note: This is when the file would print)
If your file still isn't opening, you may need to be more specific about the path to the directory. I have my directory located in C:\Users\UserName. If your directory is in C:\Users\UserName but you have it embedded within another folder, you may have to open that folder in PowerShell (you can do this by typing cd FolderName) before you can open the directory folder that's inside it. The easiest thing to do is to save your directory folder in the path C:\Users\UserName so that opening files will take less time/typing.

Python Shell - Syntax Error

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.

Can't run Python .py files from terminal on Mac

I just downloaded Python 3.2 to Mac OS 10.6 environment. I'm new to programming and am trying to run my first stand-alone .py file, but I keep getting an error message saying "no such directory or file." The name of the file is "script1.py" and I saved it to /Users/billp/Documents. When I open the Terminal to run the file I type:
python script1.py
I've also tried adding this line to the beginning of the script:
#!/usr/local/bin/python
As well as this one:
#!/usr/bin/env python
Yet, I keep getting the same error message. Any help would be greatly appreciated.
Make sure you are in the right working directory after opening terminal. Type
cd /Users/billp/Documents/
(use tab to autocomplete)
then
python ./script1.py
This way you are launching python executable and passing it path to your file as the first argument. The shebang #! line you mentioned allows you to launch your script directly, like this: ./script1.py, but you need to mark the file as executable chmod +x script1.py and provide path to interpreter (python) after the shebang. #!/usr/bin/env python references your default python installation.
The ./ stands for current directory. By default when you type script1.py your shell (which is the thing that you type commands into, through the terminal) would look for executable file in special folders listed in PATH env variable. script1.py is not usually there, so you would see -bash: script1.py: command not found. python, on the other hand is on the PATH so it should work.
Maybe you forgot to make the file executable? Try this at the command prompt:
$ chmod +x script1.py
I prefer to start my Python scripts in a Mac with these lines (assuming of course that you're saving the file in UTF-8 encoding:
#!/usr/bin/env python
#coding=utf-8
Also, make sure that the pythoncommand is available in the path. If everything is set up correctly, it won't be necessary to type python first, and you can run the script directly by typing ./script1.py in the directory where it is located.
One final thing, for running a piece of code when executing the script from the command line (as opposed to simply loading the definitions in the file), write this at the end:
if __name__ == '__main__':
# the code you want to call
Are your python binaries here?
/Library/Frameworks/Python.framework/Versions/3.2/bin/python
It's worth pointing out, as long as the file is in your current directory it's automatically available. Otherwise, anything files will have to be referenced absolutely using the full path information.
So the following examples are calling the same file:
Explicit (absolute path)
python /Users/billp/Documents/script1.py
python /Users/billp/Documents/script2.py
python /Users/billp/Documents/script3.py
Implicit (relative path)
cd /Users/billp/Documents
python script1.py
python script2.py
python script3.py
As long as you're working with files in the same directory (commonly known as your working directory), you may always safely use relative paths. If the files are anywhere else, you must always specify an absolute path.

Categories

Resources