2
votes

I have a python file that I'd like to execute using the virtual environment (venv) in the same directory as the file. I'd like it to use this venv regardless of whether the user has activated it or not to avoid accidentally running it without the proper environment. I'd like this to work on both Linux and Windows (via Git Bash).

The issue is that venv puts python under the "bin" directory on Linux but under "Scripts" on Windows and I can't seem to find a way to change this behavior when creating it. I tried creating it with Git Bash in hopes that it would "trick" python into using a bin directory instead of Scripts, but that didn't work.

The following shebang works well on Linux:

#!.venv3/bin/python

And this one works well on Windows:

#!.venv3/Scripts/python

But what will work on both? I know that one option would be to create a shell script that activates the environment based on the detected os (using $OSTYPE), but I'd like to avoid this if possible as it isn't otherwise necessary.

1
I don't know Windows, so I don't know if this is possible for the Windows side. If both side support symbolic links, you could create a link to each of the Python binaries that lives at a common path on both systems. If you can't use sym links, then a script that can be put in a common place is the only other solution I can think of,. - CryptoFool
Hey. Did this help you out? I'm just going back and checking answers I posted to see if I can help more or if I can figure out why my answer wasn't accepted or upvoted by the person who asked the question. - CryptoFool

1 Answers

1
votes

You could use the Windows version of the shebang:

#!.venv3/Scripts/python

and then create a symbolic link on the Linux side:

.venv3/Scripts -> .venv3/bin

I just tried this with one of my virtual envs. It worked fine for me. I didn't test the Windows case, but of course, that has to work as we're using the correct shebang for Windows to begin with.