[root@localhost]# cd /home/someguy
[root@localhost]# ls
Oops, now someguy can execute arbitrary code as root.Windows used to do this with DLL files and has since had to add various modes where it's either disabled or (in the default "safe DLL search mode") deprioritized in the search order: https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic...
Doing this "only when you actually run python" doesn't help. It would make it unsafe to run for example (looking at my /usr/bin) "ufw", "hg", "smbinfo", "samba-tool" or "routel" without first making sure that the current working directory (i.e. the one that you have currently set with "cd" in your shell) is not writable by any other user and doesn't contain anything unexpected.
Node.js does it this way, and it works just fine. Not sure what you think the problem is.
>Doing this "only when you actually run python" doesn't help. It would make it unsafe to run for example (looking at my /usr/bin) "ufw", "hg", "smbinfo", "samba-tool" or "routel" without first making sure that the current working directory (i.e. the one that you have currently set with "cd" in your shell) is not writable by any other user and doesn't contain anything unexpected
Why would the current working directory matter as to that?
First, if somebody has write access to your local directory you have worse issues.
They could for example overwrite the py/pyc dependencies or replace the python inside the virtual env, and their code will be executed if you run any of the items in the bin or even import those python deps.
Whether Python "looks for a .venv directory in the current working directory" or not doesn't mattter. It will just mean that you need to activate the .venv first to get those in your PATH. But that's why you had the .venv there in the first place, to activate and use it.
Whether you activate it manually or not doesn't matter...
1. One might want to run utility programs from arbitrary working directories, for example running "ls" in another user's directory (the example I gave in my first post), with "ls" being the utility program in this example.
2. One might want to implement such utility programs in Python, which is the case with e.g. "ufw", "hg" and "smbinfo" (the examples I gave in my second post).
3. If the suggestion to load Python modules from the current working directory is implemented, and a utility program is implemented in Python as in (2) and one runs it with a current working directory controlled by another party as in (1), then that party is able to execute arbitrary code as the user running the utility program, resulting in privilege escalation.
To make this more concrete, let's say we reimplement "ls" in Python as "ls.py" and install it in /usr/bin. If we then (quite reasonably, I think?) use our "ls.py" as root to list the files in /tmp, which is writable by other users...
# cd /tmp/
# /usr/bin/ls.py
then, if Python loads code from ".venv" in the current working directory, whoever created "/tmp/.venv" is able to execute code as root. $ mkdir bin_dir
$ echo 'print("mymod loaded as expected.")' >bin_dir/mymod.py
$ echo 'import mymod' >bin_dir/script.py
$ mkdir working_dir
$ echo 'print("Oops, game over!")' >working_dir/mymod.py
$ cd working_dir/
$ python3 ../bin_dir/script.py
mymod loaded as expected.> The error message SHOULD inform the user that a virtual environment is required, SHOULD provide shell-specific instructions on how to create and activate a virtual environment named .venv, and SHOULD provide a link to a documentation page that explains how to create and activate a virtual environment.
The more I program, the more I appreciate:
- 1. tools that are extremely small and functional and can be composed together (re: unix pipes, cat foo | grep bar)
- 2. things that are just files and folders on disk, so that they can get introspected, moved to different disks, symlinked, put on FUSE filesystems, etc. The common interface of file or folder is very flexible.
They're purely incidental complexity: it should be able to set up a project-local venv by themselves without me having to micromanage anything. I don't care about activating/deactivating venvs. It should just work when I navigate to the project folder. It should let me tweak where packages are stored for the 1% of the time that I care.
So why is it used? As it has a fantastic eco-system for applied statistics / machine learning / various math things. If you're teaching an introduction to programming then it's a very effective language.
That and once people progress it's decent - much prefer the code written in Python by competent developers to that written in Java or even Go. It also sets people up to use more robust languages such as C# or (for those with a Math background) Haskell.
You still need to do the setup when you create the project, but once you do you can use direnv[0] to activate/deactivate the venv[1]. I target GCP often, so I have direnv switch over to the correct project [3] also.
[1] https://stackabuse.com/managing-python-environments-with-dir...
[2] https://stackoverflow.com/questions/56781493/gcloud-is-it-po...
I find myself doing `cd .` a lot.
I mean, soon it becomes “python3.5” (don’t really try that one!), “python3.6”, “python3.7”, “python3.8”, and maybe “python3.9-pypi” and who knows what else.
If the noobs you are pandering are too lazy to create a venv, they will never be able to change your script that says
pip3.12.8a-lyrical-magical install xyzyzy
To match pip3.14q or whatever version they are running. With the venv you are much closer to a write-once, run-anywhere situation for writing Python backed and supporting scripts. python3 -m venv .venv
.venv/bin/pip install whatever
.venv/bin/python foo.pyPeople like to complain about Python, but `./venv/bin/foo` is basically identical to `./node_modules/.bin/foo` and I don't see why the former deserves so much hate.
.venv/bin/a
wouldn't be able to invoke b without activating the venv or otherwise putting .venv/bin on the PATH.https://hg.sr.ht/~twic/devtools/browse/bin/gradle?rev=tip
If i'm in a directory with a gradle wrapper, i essentially never want to run any globally installed gradle, i want to run the one in the wrapper.
It's more of a mixed bag with python. If i do:
cd ~/myproject
python main.py
Then yes, i want to use the venv in ~/myproject.If i do:
cd ~/myproject
salt '*' status.all_status
I do not want to run salt using the venv in ~/myproject!On the other hand, if i do:
python ~/myproject/main.py
I think i'd like to use the venv in ~/myproject.So maybe resolve the file that will get run, then look for a venv in the vicinity of that?
What happens when you do:
cd ~/myproject
python -m json.tool data.json
?Another option would be to introduce a new set of commands - vpython runs python using a nearby venv, vpip runs pip using it, etc. Or call the ppython, ppip, etc - less mnemonic, but faster to type.
Not doing much Python now so I don't know what the current situation is.
I like the concept potentially meaning less friction, but the implementation may make things more confusing for beginners when you're not explicitly activating an environment in your shell. I think maybe a flag to enable the behavior at runtime would be better than it'd be opt out.
So don't have many in the same directory. Since the name shall be fixed, you wouldn't be able to anyway (e.g. .venv, similar to .node_modules).
Simple as that.
pypackages is a proposal for this, but since it's harder to do, and this one easier, one step after the other...
If someone runs python it should look for a .venv directory in the current working directory. If it finds one, it should use the python interpreter located in that virtual environment instead of the system one. Obviously a flag can be used to disable this behavior and run the system interpreter no matter what the current directory is.
There’s an argument to be made that it should search upwards in the directory tree as well. In case the current directory is /home/user/myproject/otherdir/deeperdir/ and the .venv is in /home/user/myproject/.venv/ then the venv should still be used.
This not only saves a lot of trouble for users in the terminal, but also for various shell scripts, systemd service definitions, etc. Anyone who wants to run a Python program correctly just has to make sure they cd to the correct place first. Then they can just run python without having to know anything at all about virtualenvs.