When attempting to run .py files from the command prompt I get this,
C:\users\ocean>python helloworld.py
python: can't open file 'helloworld.py': [Errno 2] No such file or directory
The environment variables are already set to ;C\python27 and ;C\python32 (yes, I'm running both releases of python.)
Do I have to set a new variable? What must I do? Running Windows 7, it was a pain to get it to even run that error, before 'python' wasn't even recognized.
Edit: C:\users\oceans does not include my python folder. Is there anyway to redirect the directory so that I won't have to move all of the python files to ocean?
You have to either do:
python c:\path\to\the\file.py
or
cd c:\path\to\the
python file.py
Related
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.
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.
I just downloaded and installed Anaconda on my Windows computer. However, I am having trouble executing .py files using the command prompt. How can I get my computer to understand that the python.exe application is in the Anaconda folder so it can execute my .py files?
You should use Anaconda Prompt instead of common Windows command prompt.
Then navigate to your folder with the .py file and run:
python myfile.py
However if you want to use normal command prompt you should put the path with you're python.exe which is usually in
C:\Users\<username>\AppData\Local\Continuum\anaconda3\python.exe
behind this one put your .py file.
Launch JupyterLab from Anaconda
(Perform the following operation with JupyterLab ...)
Click on icon folder in side menu
Start up "Text File"
Rename untitle.txt to untitle.py (The name of the file started up was also changed)
Start up the "terminal" (In windows the power shell starts up)
Execute the command python untitle.py
Right click on a .py file and choose 'open with'
Scroll down through the list of applications and click something like 'use a different program'
Naviage to C:\Users\<username>\AppData\Local\Continuum\anaconda3
click on python.exe and then click on 'ok' or 'open'
Now when you double click on any .py file it will run it through Anaconda's interpreter and therefore run the python code.
I presume if you run it through the command line the same would apply but perhaps someone could correct me?
Just get to the home of jupyter notebook and select "New" then select "Text file".
Then save the text file as file_name.py
Write your code in the file and save the file.
Then open the "Anaconda Prompt" and then type as follows to run your file
python file_name.py
You can do it from the "Anaconda Prompt"
conda run "my_script.py"
I was doing exactly as Martin Bosch suggested, and was getting the following:
(base) C:\>python command.py
python: can't open file 'command.py': [Errno 2] No such file or directory
I solved it this way:
navigate to the exact file location using the "cd" command
for me this was:
(base) C:\>cd my_scripts
this should put you specifically in the file where your .py script is located.
now you should try to input the name of your file.
(base) C:\my_scripts> test_script.py
you may get asked which program to run this with, and simply find python.exe
After doing this process once, I can simply type (in anaconda prompt)
test_script.py
and it runs no problem, even from the top of the file tree (I don't have to be in the exact file, nor do I have to explicitly give the whole file path)
Anaconda should add itself to the PATH variable so you can start any .py file with "python yourpythonfile.py" and it should work from any folder.
Alternatively download pycharm community edition, open your python file there and run it. Make sure to have python.exe added as interpreter in the settings.
If you get the following error:
can't open file 'command.py': [Errno 2] No such file or directory
Then follow this steps to fix it:
Check that you are in the correct directory where the Python file is.
If you are not in the correct directory, then change the current working directory with cd path. For instance: cd F:\COURSE\Files.
Now that you are in the directory where your .py file is, run it with the command python app.py.
Check where is the directory for the ananconda environment directory which is generally
"C:\Users\[UserName]\.conda\envs\[conda environment directory]"
You will see python.exe in that directory.
After that, you need to use the following command to execute your python file (i.e. xx.py) when you are running Anaconda prompt and you will be done:
"C:\Users\[UserName]\.conda\envs\[conda environment directory]\python.exe" xxx.py
BTW, if you have global variable (i.g. variable yyy) that contain directory, you have to define the global variable that contains full path of directory just below the header (the import section) to prevent the "name 'yyy' is not defined" error to occur:
from pathlib import Path # dealing with path issue
yyy = Path("[DriverLettter]:\Full\Path\of\Directory")
I have a python executable I wish to run from PowerShell using the command python [executable].py.
First I changed the directory in PowerShell to the location of the executable using cd path\to\my\directory which worked fine. However whenever I tried to use python to execute my code, PowerShell immediately searches for the [executable].py in Python's installation folder - fails to find it - and gives the an error that it cannot find the appropriate file.
How do I make sure that Powershell looks for the executable in the directory I indicated as opposed to the default Python installation folder?
If you want to run python.exe from a location other than the installation directory you'd call it with its full path:
& 'C:\path\to\python.exe' 'your.py'
If you want to run it from the current directory, prepend the filename with the relative path .\:
& .\python.exe 'your.py'
If you call an executable without a path like this:
& python.exe 'your.py'
PowerShell will look for a matching file in the directories listed in the $env:PATH environment variable, and execute the first match (or report an error if no matching file can be found).
With that said, the error you got in your screenshot is not because of the Python interpreter, but because of the file you want the interpreter to run. You're calling
python conditions
when you actually want to run
python conditions.py
Neither PowerShell nor Python magically add the extension for you. Instead they report an error because a file conditions (without an extension) simply doesn't exist.
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.