7

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.

Ajay Gautam
  • 997
  • 12
  • 14
  • Did you ever figure this out? – Tom Leese Feb 06 '16 at 16:34
  • Nope. Workaround - add /usr/local/bin to the Run config. – Ajay Gautam Feb 16 '16 at 17:53
  • @AjayGautam, can you be specific what you mean with add X to the Run config? That could even be an answer... – Jorge Leitao Sep 09 '16 at 16:50
  • @J.C.Leitão Press "command + shift + a" in Pycharm -> input "edit config" -> select the first result -> select "Python" on the pop window -> select your script -> You can see "Environment variables" on the right, then you can make it work. By the way, I try to set "/usr/bin/local" as default "Environment variables" in the "Defaults" but it doesn't work. – Kxrr Sep 21 '16 at 17:20

1 Answers1

0

This was reported as a bug to JetBrains, PY-17816. It is now fixed as of the 2017.1.2 release (version 171.4249.47) per the release notes.

Harold Short
  • 171
  • 9