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.
Related
I have a script called "submit.py" and it begins with "#!/usr/bin/env python.exe" which works if I'm inside the directory containing "submit.py". I can run "./submit.py" and the script runs properly. I decided to add the directory containing "submit.py" to my PATH environment variable so I can run "submit.py" from any directory.
However, I cannot run "submit.py" from any directory. The error I get is:
D:\Program Files\Python36\python.exe: can't open file '/mnt/d/Program Files/kattisTools/submitToKattis': [Errno 2] No such file or directory
I believe the error has to do with the differences in how Windows Python expects a path to be and how WSL handles paths. What I mean is python.exe should be looking for "D:\Program Files\kattisTools\submitToKattis" but WSL is feeding it "/mnt/d/Program Files/kattisTools/submitToKattis"
I also believe that "./submit.py" works when I'm inside the directory containing submit.py because the '.' operator is handled differently in WSL that feeds the real Windows path to Windows Python.
I was hoping there's a remedy so that I can run "submit.py" from any directory with it relying on Windows Python?
06/26/2018 Update: I have looked into Shared Environment Variables between WSL and Windows, and particularly the '/w' flag. I haven't gotten this to work the way I wanted yet but it might be something?
did you get this to work by now?
Having the same issue here when calling a python script with a pipenv virtual environment (windows based python.exe) from within the wsl :(
You are completely right....
Calling the script directly from its folder it looks like that:
C:\scripts\.venv\Scripts\python.exe ./test.py
and it works
while it looks like that (and does not work) if it is called from another folder:
C:\scripts\.venv\Scripts\python.exe /mnt/c/scripts/test.py
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")
Hi I noticed that whenever from the command line (using windows 8.1) I type
python file.py
It automatically knows that I meant to write python.exe file.py
How does it do this?
I installed Anaconda, and I understood I have an environment variable pointing to python.exe. But that doesn't explain why I need not type python.exe everytime.
This is not a python feature. The behavior to call executables without file extension is defined by the operation system and the PATH variable. Wikipedia has a good answer to your question
PATH (variable)
...
When a command is entered in a command shell or a system call is made by a program to execute a program, the system first searches the current working directory and then searches the path, examining each directory from left to right, looking for an executable filename that matches the command name given.
...
The file name after the executable could be everything. So if you want you can call python demo.txt. If the file content is readable for python it would also be executed.
Python is searched for using the path. Find the PATH in the list of environment variables.
If you do not write python and simply double-clicks the file, the registry is searched.
You can see what and associate files with program using ftype and assoc from the command line. See e.g. http://www.fileformat.info/tip/microsoft/assocftype.htm
If you omit the extension the registry is also searched. This cmd shell searches the environment variable PATHEXT and the registry for finding python.exe. After this, it is using the registry to find the location.
When you register your python distribution from within Spyder, these changes are made.
You can reveal this information by using ftype and assoc like I wrote in the beginning.
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
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.