1st Python file creation in Notepad on Windows getting error - python

So I've read through countless people asking similar questions but none seemed to be the answer I was needing.
Tonight I started the W3 Python tutorial. I downloaded the latest version of Python.
The first task is to go into a text editor, in this case I chose Notepad. I copied the following:
print("Hello World!")
and saved that as helloworld.py
I made sure it was in my C: drive... which is the only one I have anyway.
I used cmd.exe and typed in C:\Users\MyName>python helloworld.py
And I get the error: python: can't open file 'C:\Users\18327\helloworld.py': [Errno 2] No such file or directory
It is saving into my documents folder, is that the issue?

Your file's directory is 'C:\Users\18327\Documents\helloworld.py', while your directory specified in cmd is 'C:\Users\18327\helloworld.py'. Instead, run cmd in the Documents folder.

Related

Cannot open .py files: "[Errno 2] No such file or directory"

I cannot open any .py file: when I run in the command prompt either "python test.py" or "python3 test.py" or "py test.py", it just says can't open file 'C:\Users\Ciela\Desktop\test.py': [Errno 2] No such file or directory.
Python is installed, latest version
All other versions are uninstalled
Python was automatically added to PATH during installation, I can see it in both User and System paths and the version is correct
the files can be opened in Python just by double-clicking them, although they shut off immediately (I know they work because the "turtle module" screen persists on the screen)
The OS is Windows 10 and I am a total noob trying to learn
What could it be??
I would try first:
cd C:\Users\Ciela\Desktop\
in command prompt then I would try it again
I sorted it out. For anyone struggling with the same issue, the problem might be OneDrive. Windows10 automatically creates 2 desktops: the one in User, and the one in User/OneDrive, where files are stored by default. Essentially I was looking for the files in the wrong desktop folder.

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.

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.

How do I read a text file in Pydev?

I'm doing a school assignment that requires me to read a file. I chose to use the Eclipse and Pydev combination because I really like the environment. However, no matter where I place the .txt file I get the following error: FileNotFoundError: [Errno 2] No such file or directory: 'file.txt' I've tried placing it in the src folder and the file directory. Something that I find quite odd is that if I were to run the same python module in IDLE, the file is found and read perfectly. Any help is appreciated!
You should put the file in the same folder as your .py file, if that doesn't work try to find out what directory it's currently using os.getcwd().
To fix this specifically:
Open Run Dialog...-> Select your run configuration->Arguments Tab->Working directory
EDIT:
import os
print os.getcwd()
If you cannot determine where to place your file - place it anywhere and use absolute path:
print(open(r'c:\your_file.txt', 'r').read())
If you've not played with your eclipse too hard you can just place your_file.txt in your project directory and use:
print(open('your_file.txt', 'r').read())
Update:
From one of your comments I've seen that you are doing sth extremely weird - look here:
You've somehow managed to open python.exe in your console window within eclipse and you edit files with eclipse but run it from python.exe directly on right hand side hence your code on the left has different environment if executed from eclipse and different when executed from right handside window.
Close that console and push PLAY button in your ECLIPSE:
You better first edit your files in something easier like notepad++. Just create your python source within directory whrer your python.exe resides and run that from command line with:
python.exe yoursource.py
Once you get a hang of how things work - switch to eclipse.
While working in notepad++ follow python tutorial
You simply are trying to do too many unknown to you things at the same time - this way you'll be scratching your head all day long
Pydev in Eclipse default working directory
Took me 10 seconds to find ;)
When you run your script from console it's this file directory, but from eclipse it could be another.

Categories

Resources