Jennifer
Jennifer

Reputation: 165

bash python no such file or directory: why is my file path different from what I specified?

I'm trying to execute a shell script on Windows 10. I've also turned "Windows Subsystem for Linux" feature on, because the script needs to run a package that asks me to do so during installation.

My path for my programs as: export PATH=$PATH:/Users/Myname/Desktop/Myfolder

Then I tried to run the shell script I have stored in a specific folder (and directly called the working directory) by:

python /Users/Myname/Desktop/Myfolder/Mysubfolder/create_fa.py

But this gives me this error:

C:\Users\Myname\AppData\Local\Programs\Python\Python38-32\python.exe: can't open file 'C:/Program Files/Git/Users/Myname/Desktop/Myfolder/Mysubfolder/create_fa.py': [Errno 2] No such file or directory

I'm confused because I specified where my create_fa.py file was stored, but when I run the shell script, it seems like it's trying to look my create_fa.py file at a different directory from which I specified. Why is the script looking for a different directory?

Upvotes: 1

Views: 9108

Answers (2)

b_c
b_c

Reputation: 1212

I'm running a shell script (.sh), and when I run the script, it runs with "Git for Windows"

This is where the path "corruption" is coming from.

The Git Bash terminal creates a "linux-esque" directory hierarchy (sort of), with / (the "root" directory) set to C:/Program Files/Git. So, when you use the path /Users/Myname/Desktop/Myfolder/Mysubfolder/create_fa.py, Git Bash resolves that to C:/Program Files/Git/Users/Myname/Desktop/Myfolder/Mysubfolder/create_fa.py, as you've seen.

From Git Bash, you can access your normal directories like this:

  • ~ = %USERPROFILE% (your user directory)
  • /c/... = Directories under C:\, so your desktop would be /c/Users/username/Desktop

Upvotes: 1

r.ook
r.ook

Reputation: 13878

The problem is python doesn't look at $PATH at all. If you have script.py in multiple PATH folders, how do you expect python to differentiate which script.py to run?

What python does is look into the current directory, i.e. C:/Program Files/Git/ You'll need to step back out that folder for a relative path, or supply the absolute path.

e.g. if your bash session is currently at /Users/Myname/ then all you need to do is:

python Desktop/Myfolder/Mysubfolder/create_fa.py

Or pass in your user folder as absolute path:

python ~/Desktop/Myfolder/Mysubfolder/create_fa.py

Upvotes: 1

Related Questions