Search code examples
python-2.7mingwmingw-w64

Specifying multiple PYTHONPATH values in MinGW


Say I've got a file (foo.py) that prints the sys.path:

# from path.to.folder import foo
# from another.path.to.folder import bar

import sys

for p in sys.path:
    print(p)

My issue is that you uncomment the import statements and run the file through MingW64 (in my case, Git Bash) - Python will not be able to locate the modules correctly, yielding an ImportError.

Running the file with the imports commented out yields this output:

user@host MINGW64 /
$ export PYTHONPATH="C:\path\to\folder:C:\another\path\to\folder"
$ python foo.py

C:\some\path
C:\path\to\folder:C:\another\path\to\folder
C:\Windows\system32\python27.zip
C:\Python\2.7.12\DLLs
C:\Python\2.7.12\lib
C:\Python\2.7.12\lib\plat-win
C:\Python\2.7.12\lib\lib-tk
C:\Python\2.7.12

The problem seems to be that MinGW is not interpreting export PYTHONPATH="C:\path\to\folder:C:\another\path\to\folder" as two separate paths. It passes a single malformed path to Python, which doesn't know what to do with it.

MinGW's Posix path conversion documentation does not appear to cover this scenario, but I would think this is an incredibly common use case.

How do I set the PYTHONPATH environmental variable in MinGW such that it sends two paths along to Python?


Solution

  • Turns out the Posix path conversion documentation does cover this case:

    Arguments containing a ; are considered to be Windows path lists and are not converted.

    Converting the middle colon to a semi-colon fixes the issue:

    export PYTHONPATH="C:\path\to\folder;C:\another\path\to\folder"