I have a small python script

#!/usr/bin/env python3

import some_python3_module

def main():
    # do stuff

if __name__ == '__main__':

and cannot run this script with Python3, since ROS sets the PYTHONPATH variable to some version 2.7.-related locations, meaning Python 3 cannot find any modules in its dist-packages directory. I would like to override this behaviour without having to modify the outside envorinment. That is, I wish to unset PYTHONPATH, but only for this script, and preferably from within it, so that the shebang will still work. Is this possible?

Not sure if this is better suited to superuser.com

2 Answers 2


There is a command line option that suits your needs:

#!/usr/bin/env python3 -E


Ignore all PYTHON* environment variables, e.g. PYTHONPATH and PYTHONHOME, that might be set.


I know this question is a bit old but I was looking around for the specific solution I found for this problem: It turns out this is the actual purpose of using /usr/bin/env

#!/usr/bin/env -u PYTHONPATH python3

This accomplishes the desired unset, then runs python3

From man 1 env:

ENV(1)                                    User Commands                                    ENV(1)

       env - run a program in a modified environment
       -u, --unset=NAME
              remove variable from the environment

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .