0

I am trying to write a file in the/usr/bin path so that my program can be ran from anywhere.

This is the code

os.chdir("/usr/bin")
writetofile("slang", "cd " + pdir + " && python3 slang.py $1 $2 $3")

And pdir is set with os.getcwd()

However, it gives me an error. FileNotFoundError: [WinError 3] The system cannot find the path specified: '/usr/bin'

I tried removing the "/" but that didn't work either.

And I can't include the full path to /usr/bin because the MSYS2 path might not be the same for everybody.

How do I get Python to change the CWD to /usr/bin?

After running python -m site I get this output:

sys.path = [
    'C:/msys64/home/*me*',
    'C:/msys64/mingw64/lib/python310.zip',
    'C:/msys64/mingw64/lib/python3.10',
    'C:/msys64/mingw64/lib/python3.10/lib-dynload',
    'C:/msys64/mingw64/lib/python3.10/site-packages',
]
USER_BASE: 'C:/Users/*me*/.local' (doesn't exist)
USER_SITE: 'C:/Users/*me*/.local/lib/python3.10-mingw_x86_64/site-packages' (doesn't exist)
ENABLE_USER_SITE: True
OK9090
  • 25
  • 5
  • This looks like you're using a native Windows `python.exe`, not one compiled with msys. You can't see synthetic directories under names that msys synthesizes unless your Python interpreter itself was compiled with msys. – Charles Duffy Nov 02 '22 at 22:38
  • If you don't have permissions, things often look like they don't exist. My guess is your std user doesn't have write permissions in /usr/bin. As... you shouldn't (it would be a big security hole). – JL Peyret Nov 02 '22 at 22:40
  • @JLPeyret, what makes you think the OP is trying to _write_ to `/usr/bin`, vs just `chdir()` there? (They can very much get this problem just trying to use `chdir()` if they're on Windows, because Windows _doesn't have_ a `/usr/bin`). – Charles Duffy Nov 02 '22 at 22:40
  • probably that their user can't write there unless they are in an elevated permission mode (i.e. maybe an admin on the cli). – JL Peyret Nov 02 '22 at 22:41
  • Maybe you can read the OP? *I am trying to write a file in the/usr/bin* – JL Peyret Nov 02 '22 at 22:42
  • _Ahh_. Thank you for calling that out -- I was indeed focusing on the title -- but that would be a different error message: an EPERM / "permission denied", not "cannot find the path specified" – Charles Duffy Nov 02 '22 at 22:43
  • Well, I really don't know. This whole thing looks fishy, and yes, I as wondering about /usr/bin on Windows too. The OP can go and digest these comments and see if they need to adjust anything. – JL Peyret Nov 02 '22 at 22:44
  • @JLPeyret, the other thing is that the directory mapped to `/usr/bin` is typically owned by whoever installed the copy of msys on Windows, unless they installed it as Administrator, so having write there is not so unusual as it would be on a UNIX system. – Charles Duffy Nov 02 '22 at 22:44
  • @JLPeyret (...if you're not familiar with msys, it's a lightweight competitor to cygwin -- a compiler toolchain and standard C library that acts like a UNIX libc even on win32, and presents a UNIXy-looking filesystem and set of APIs to applications that were built against it; perhaps in that context the question makes more sense? It's what "Git Bash" is built on, and thus rather widely used) – Charles Duffy Nov 02 '22 at 22:47
  • @JLPeyret I am using the python3 that I installed with pacman, so I don't think there should be a permission error. Plus, I can write into /usr/bin with a Shell Script file, so why not a Python script? – OK9090 Nov 02 '22 at 23:06
  • @OK9090 I am going to defer to Charles on that - I think they truly have a better handle than me here. But perhaps you can 1) post a **minimal** example of your Python code failing? with the error message? 2) the path of the exe for the Python that is being used (`python -m site` may help here) - it may very well be a windows Python vs msys Python thing and 3) perhaps you can try writing a file /usr/bin under Windows? Right now, we have an error on a python script, but no Python code to be seen, nor a way to judge if it's git bash Python or Windows Python. Welcome aboard. – JL Peyret Nov 02 '22 at 23:21
  • @JLPeyret I added stuff that will hopefully help, if you need more just tell me. – OK9090 Nov 02 '22 at 23:30
  • disclaimer: not a Python-on-git-bash guy. But normally, on Linux/macos you'd set that information on the script itself, via a shebang, rather than a shell that cd's and runs. This is a question here about [shebangs and Windows](https://stackoverflow.com/questions/7574453/shebang-notation-python-scripts-on-windows-and-linux). googling "shebang git bash python" may give you some idea. a typical shebang: `#!/usr/bin/env python` Again, your approach *may* be the better one, but I can't really help with it - I've run Win Python on Win, never Unix Python on Win. good luck. – JL Peyret Nov 03 '22 at 00:17
  • You shouldn't touch `/usr/bin`, it should be controlled by your package manager. Either put the program into `/usr/local/bin` (does it exists in MSYS2?), or into some other directory, which you then add to the PATH. – HolyBlackCat Nov 03 '22 at 19:54
  • @HolyBlackCat /usr/local/bin does not exist in MSYS2. – OK9090 Nov 03 '22 at 20:26

1 Answers1

0

The only solution I've found is creating a separate shell script file (with the code to write into /usr/bin), and then running that script in a subprocess.

OK9090
  • 25
  • 5