How to set PYTHONPATH and possible effects?

I’m using Blender in my project which has it’s own python implementation separate to that of my environment. When I run guild with my project it writes to PYTHONPATH which interferes with sys.path and causes problems with the blender installation of python. Is there any easy way to set the PYTHONPATH in my guild files to overcome this issue? Also are there any problems that might occur from setting PYTHONPATH while guild is running? Thanks.

Guild modifies the system path by inserting the run source code directory at the front of the path. This can cause undesirable results if you’re unintentionally copying source code files for a run that collide with system packages and modules.

You can show the files that Guild copies as source code by running:

guild run <operation> --test-sourcecode

See Operation Source Code for more information.

You can disable this behavior altogether by disabling source code for your operation. In this case Guild doesn’t modify the Python system path at all.

However, in this case you lose the feature of creating source code copies of your project.

Regarding modifying the PYTHONPATH environment variable while Guild is running, this should not be an issue, though typically this won’t affect module loading as (I believe) once the Python VM is running it uses sys.path exclusively and ignored the PYTHONPATH environment. The environment variable would come into play for calls to subprocess.Popen when env is not otherwise specified, which may be important for Blender.

If you can identify the specific problem that’s occurring with Blender I can offer a more specific solution.

P.S. If you want the painful details of Guild’s behavior wrt to PYTHONPATH, see these tests.