Search code examples
pythonvirtualenv

how to make use of Python modules in virtual environments?


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.


Solution

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