WSL -- Command Not Found

Viewed 3464

I'm running the following command in PowerShell wsl pipenv run python ~/test.py and receive the following error /bin/bash: pipenv: command not found. If I launch WSL/Ubuntu, I can run pipenv run python ~/test.py and get a successful output. In PowerShell and Git Bash, I can run pipenv -h and get proper output. Where does WSL look for this command?

On a related note, I can run python ~/test.py and python3 ./test.py when I launch WSL/Ubuntu and get successful output. But from PowerShell, only wsl python3 ~/test.py works. When I run wsl python ~/test.py I receive the same command not found error. What am I missing?

1 Answers

Based on our comments exchange, the problem is that the directory in which the pipenv executable is located - /home/<username>/.local/bin/ - isn't present in the PATH environment variable when invoking a command non-interactively.

This suggests that it is an initialization file that extends PATH, and it seems that it is ~/.bash_profile in your case; to force an explicit bash invocation to source this file, the -l (--login) option must be passed.

You report that the following command ultimately worked for you:

bash -l -c "cd ~ && pipenv run python ~/test.py"

Note:

  • An explicit invocation with -c is what wsl.exe does behind the scenes when you pass it a command to execute, albeit without -l (e.g. wsl date turns into bash -c date).

    • The use of -c makes the shell non-interactive, which means that ~/.bashrc isn't sourced either; if you want the latter, add -i
  • Without a command (executing just wsl.exe or bash.exe), an interactive bash shell is entered, which - in effect - does use -l, and therefore automatically sources ~/.bash_profile (wsl.exe / bash.exe use the alternative mechanism for requesting a login shell: the first entry of the argument array (which you can inspect with echo $0) is set to -bash, and the initial - tells Bash to start a login shell).

See also: Thanks, Zymotik.

Related