0

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:

  1. How do I tell my .py file to import modules from a specific virtual environment and not from the main installation?
  2. Can I import a module from a virtual environment and another from the main installation (ex: I want to use a method which is no longer available in the latest version of some module)?
  3. What happens if I import into a script using one virtual environment, a script which uses modules from another virtual environment, and some dependencies are overlapping (ex: one uses pandas 1.0.3, the other uses pandas 1.0.0)? Is the overlap handled or do they drop to the same version (if so, which one?)?

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.

asked Mar 27, 2020 at 16:05

2 Answers 2

2

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.

  1. A virtual environment takes the place of your "main installation" when you're running a python script, if you activate that virtual environment. So, once a virtual environment is active, it will always look at the virtual environments installed modules and not the "main" installation.
  2. 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

  3. This isn't how virtual environments work, as discussed above.

answered Mar 27, 2020 at 16:32
Sign up to request clarification or add additional context in comments.

Comments

0
  1. 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.

  2. 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.

  3. 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.

answered Aug 2, 2024 at 17:38

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.