Running this on Mac OS X El Capitan 10.11.1 in PyCharm 5 (This was working fine in PyCharm 4.5)
import os
print("PATH:", os.environ.get("PATH"))
If I run this program with PyCharm's project Interpreter set to System's Python: /Library/Frameworks/Python.framework/Versions/3.4/bin/python3
, here is the output I get:
PATH: /Users/agautam/bin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
If I run the same program with project interpreter set to a virtual environment /Users/agautam/work/my-awesome-py-project/venv/bin/python3
I get:
PATH: /usr/bin:/bin:/usr/sbin:/sbin:/Users/agautam/work/my-awesome-py-project/venv/bin
The Issue: is that /usr/local/bin
goes missing when I use a virtual environment.
Here is how the virtual env is created:
import sys, platform, subprocess
from os.path import dirname, join
root_path = join(dirname(__file__), '../..')
venv_path = join(root_path, 'venv')
def build_virtual_environment():
print("Building virtual env from Python version", sys.version)
# Create a fresh virtual env
import venv
builder = venv.EnvBuilder(with_pip=True)
builder.create(venv_path)
# Install dependencies in new virtual env
run_in_venv('python', ['-m', 'pip', 'install', '--upgrade', 'pip']) # Upgrade pip itself
run_in_venv('pip', ['install', '-r', join(root_path, 'requirements.txt')])
def run_in_venv(cmd, args):
if platform.system() == 'Windows':
cmd += '.exe'
virtual_env_bin_path = r'venv/Scripts'
else:
virtual_env_bin_path = r'venv/bin'
subprocess.check_call([join(root_path, virtual_env_bin_path, cmd)] + args)
if __name__ == '__main__':
build_virtual_environment()
Additional info: Running the python interpreters from the command line produce exact same results (so it seems its a pycharm issue):
$ /Library/Frameworks/Python.framework/Versions/3.4/bin/python3
Python 3.4.0 (v3.4.0:04f714765c13, Mar 15 2014, 23:02:41)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ.get("PATH")
'/Users/agautam/bin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin'
>>>
$ ./venv/bin/python3
Python 3.4.0 (v3.4.0:04f714765c13, Mar 15 2014, 23:02:41)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ.get("PATH")
'/Users/agautam/bin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin'
>>>
Any help / information / pointers will be highly appreciated.