简体   繁体   中英

Import module with virtual environment in Python

so here is my question:

Script A imports Script B , Script A uses Virtual Environment A and Script B uses Virtual Environment B .

import sys

interpreterPath = "[...]/.env/bin/python"


if sys.executable != interpreterPath:
    import subprocess

    print("Creating subprocess...")
    subprocess.Popen([interpreterPath,__file__])
    print("Subprocess has exited.")
else:
    ############################
    ###   SUBPROCESS START   ###

    import module

    # TODO

The importing of the module after "SUBPROCESS START" works fine, it just does not return functions I define where the "TODO" is located at.

How would I archive this? Thanks for helping.

In principle, it is a very common scenario that you describe: most non-trivial projects depend on other libraries that have dependencies themselves. However, you need to differentiate here properly between dependencies (ie other libraries) and virtual environments, which are a means to install those dependencies in an isolated manner.

So coming back to your question, your project A should depend on project B and you run both in project A's virtual environment.

How that is implemented in detail, depends a bit on how your projects are set up.

Think of virtual environments like "working boxes" for projects ie when you're within one virtual environment, you're isolated from any others you may have. Virtual environments allow you to manage dependencies and ensure you have no version conflicts etc. between other projects on the same machine.

It's important to understand the difference between what I'm referring to as a project vs an individual script .

In this scenario, where you have 2 scripts, but one is importing modules from another one, you really have one project which contains 2 scripts. The fact that the two scripts have different requirements doesn't matter, since you need to run both , ie need one virtual environment.

By defining virtual environments for the scripts separately, you're not actually using the virtual environment for the script which is referenced from the main one.

Links you might find helpful:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM