I'm trying to run a python script in WSL that runs Ubuntu.
In WSL, the following command does not work:
admin:/mnt/c/Users/admin$ /mnt/c/Users/admin/repo/env/Scripts/python.exe /mnt/c/Users/admin/repo/script.py
C:\Users\admin\AppData\Local\Programs\Python\Python38-32\python.exe: can't open file '/mnt/c/Users/admin/repo/script.py': [Errno 2] No such file or directory
However, the following individual commands work:
admin:/mnt/c/Users/admin$ /mnt/c/Users/admin/repo/env/Scripts/python.exe repo/script.py
admin:/mnt/c/Users/admin$ /mnt/c/Users/admin/repo/env/Scripts/python.exe
admin:/mnt/c/Users/admin$ cat /mnt/c/Users/admin/repo/script.py
Why am I seeing this strange behavior? How do I run a python script using absolute path?
I am trying to run a python programme called helloWorld.py from the command line on MacOS.
I am typing into bash:
Ryans-MacBook-Air:~ ryanunderwood$ python3 helloWorld.py
But I am getting the following:
/Library/Frameworks/Python.framework/Versions/3.7/Resources/Python.app/Contents/MacOS/Python: can't open file 'helloWorld.py': [Errno 2] No such file or directory
There is definitely a file with this name in the directory. Why is this programme not running?
Try ./helloworld.py or python3. /hello World.py
And make sure you have the right permissions
I'm trying to run a python script from another python script on a Raspberry Pi 3 with Raspbian. I've been trying to find ways to do this for some hours and didn't find anything that worked. I've tried some ways but it either says that has not permission to execute the file or it can't find it. I don't know what I'm doing wrong. I need to run multiple instances of the other script through the main script in a new console (new processes) and keep them running (I don't expect them to return anything to the main script). Can anyone help me? Because with Windows it was really easy as the program was working fine until I tried to run it on Linux (with Windows, I used os.startfile).
In test.py:
print("test1")
input()
In main.py:
import os
import subprocess
print("main")
os.system("python test.py")
input()
In the console:
main
python: can't open file 'test.py': [Errno 2] No such file or directory
In main.py:
import os
import subprocess
print("main")
subprocess.Popen("python test.py",shell=True)
input()
In the console:
main
python: can't open file 'test.py': [Errno 2] No such file or directory
In main.py:
import os
import subprocess
print("main")
subprocess.call("python test.py",shell=True)
input()
In the console:
main
python: can't open file 'test.py': [Errno 2] No such file or directory
I tried more ways but I don't remember them. Maybe I'm doing something wrong?
EDIT: I can now run the scripts without any problems with os.chdir (thanks to J H). My problem now is that it prints test in the same console window as the main.py and I needed it to create another process for the test.py. Any solutions?
EDIT 2: Finally I could start a new processes of the test.py from the main.py! I used os.system('xdg-open "test.py"') to open test.py with the default application. Anyway thanks to J H, otherwise it would continue to say file not found.
Final main.py:
import os
print("main")
os.chdir('/home/pi/Desktop/')
os.system('xdg-open test.py')
input()
Thanks in advance!
Printing out os.getcwd() will help you to debug this.
Either supply a fully qualified pathname, /some/where/test.py, or use os.chdir('/some/where') before executing test.py.
I have a self-installed python in my user directory in a corporate UNIX SUSE computer (no sudo privilege):
which python
<user>/bin/python/Python-3.6.1/python
I have an executable (chmod 777) sample.py file with this line at the top of the file:
#!<user>/bin/python/Python-3.6.1/python
I can execute the file like this:
python sample.py
But when I run it by itself I get an error:
/full/path/sample.py
/full/path/sample.py: Command not found
I have no idea why it's not working. I'm discombobulated as what might be going wrong since the file is executable, the python path is correct, and the file executes if I put a python command in the front. What am I missing?
EDIT:
I tried putting this on top of the file:
#!/usr/bin/env python
Now, I get this error:
: No such file or directory
I tried this to make sure my env is correct
which env
/usr/bin/env
EDIT2:
Yes, I can run the script fine using the shebang command like this:
<user>/bin/python/Python-3.6.1/python /full/path/sample.py
Your file has DOS line endings (CR+LF). It works if you run python sample.py but doesn't work if you run ./sample.py. Recode the file so it has Unix line endings (pure LF at the end of every line).
Try using #!/usr/bin/env python as described in this post. Let the OS do the work.
I have a python script that reads and writes to files that are located relative to it, in directories above and beside it. When I run my script via Cygwin using
python script.py
The program works perfectly. However, when I run it by navigating through the windows GUI to my file and double clicking, I get a blank cmd prompt and then my program runs fine until I reach the point where I need to access the other files, at which point it fails and gives me this message in the cmd prompt that opens itself:
../FFPRM.TXT
../2025510296/FFPRM_000.TXT
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\rbanks\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1549, in __call__
return self.func(*args)
File "C:\Users\rbanks\Desktop\TSAC\EXECUTABLE\T-SAC_GUI.py", line 705, in run_exe
invalid_entry, output_text = self.apply()
File "C:\Users\rbanks\Desktop\TSAC\EXECUTABLE\T-SAC_GUI.py", line 694, in apply
p = subprocess.Popen(['cp', output_file_path, output_file_path_id])
File "C:\Users\rbanks\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 950, in __init__
restore_signals, start_new_session)
File "C:\Users\rbanks\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 1220, in _execute_child startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
I am deploying this script as well as the directory structure as a zip for users to be able to unzip and use anywhere on their PC, so it is important for me to be able to run it with a simple double click and my relative file paths.
My first thought was the cmd prompt that was opening and executing my script was in a different environment, but when I run:
cd
pause
in a .cmd script, I get:
C:\Users\rbanks\Desktop\TSAC\EXECUTABLE>pause
Which is the correct location.
I am not having any luck with Google, I assume because I can't seem to construct a sufficient search query. Could someone point me in the right direction please?
[edit] the other answer is correct(at least i suspect) but i will leave this here in the hopes that it helps the op in the future with path problems ... and doing something like this is just generally good practice
use
BASEPATH = os.path.abspath(os.path.dirname(__file__))
at the top of your script
the later
txt_file = os.path.join(BASEPATH,"my_file.txt")
or even
txt_file = os.path.join(BASEPATH,"..","my_file.txt")
this gives you the benefit of being able to do things like
if not os.path.exists(txt_file):
print "Cannot find file: %r"%txt_file
which will likely give you a better idea about what your problem might actually be (if its simply path related at least)
The problem is not the current directory. It is correct when double clicking on the icon
The problem is: Cygwin commands are not in the windows path
You are using python, so don't run simple copy commands like this, which make your script non-portable and subject to variations, requiring installation of cygwin, etc...
p = subprocess.Popen(['cp', output_file_path, output_file_path_id])
can be replaced by
import shutil
shutil.copyfile(output_file_path, output_file_path_id)
shutil module complements os module for file manipulation functions that aren't native in the operating system.
now you have a 100% pythonic solution, native, which will throw exceptions if cannot read/write files, so fully integrated in the rest of your program.
Before running an external command from Python make sure that no python way exists. There are so many useful modules out there.
Other examples of how to avoid running basic commands from python (of course if you need to run a C compilation it's different!):
zipfile package: much better than running zip.exe
gzip package: can open gzipped files natively from python
os.listdir() instead of parsing the output of cmd /c dir /B
os.remove() instead of calling rm or del
etc... python rules!