Reputation: 1237
I am tying to understand how Python virtual environments work. I get the need for it, with different installations and different versions.
My questions are:
I tried to experiment, but I couldn't find in the documentation anything besides creation and package installation.
Edit: Note: For me, even a partial answer (at least one question), will be useful and accepted, until a mode complete answer is provided.
Upvotes: 0
Views: 907
Reputation: 324
You may be under the impression that you need the activate
shell script to activate the virtual environment, but this is not so. The environment is active in a script simply as an effect of running the script with the right python interpreter, i.e. the one from the bin
directory of the virtual environment. So all you need to do is to hardcode the full pathname of the interpreter in the shebang line of the script.
No, you can't do exactly what you're asking, as @c-fennell writes. But when you install a package in the virtual environment you can restrict which version you install. The exact way to do it depends on the tool -- with pip
it can be set on the command line, with some of the more complex tools it needs to be in a configuration file.
Unlike some other development environments (node.js I think?) python venvs do require a unique version of each package. Handling dependency conflicts and finding a global solution for all the package versions is the main purpose of higher level packaging tools -- such as poetry
, pipenv
or pdm
.
Upvotes: 0
Reputation: 1032
I'm going to try to take a crack at answering these, I do think you would benefit more from reading some virtual environment documentation.
No, once the virtual environment is active, you can't access the methods from the "main" installation of a module. Python's default behavior when creating virtual environments will not include any of your existing site packages from your "main" installation in the virtual environment.
What happens if I import into a script using one virtual environment
This isn't how virtual environments work, as discussed above.
Upvotes: 2