Skip to content

virtualenv site-packages not automatically included in sys.path #184

@snickell

Description

@snickell

I'm trying to use a virtualenv with pycall. Python is loaded correctly, but sys.path does not include the site-packages from the virtualenv. Instead site-packages from the underlying python interpreter (in /opt/homebrew) is used.

For example, lets say I have a new version of urllib3 in my virtualenv:

ENV['PYTHON'] = "/Users/seth/src/code-dot-org/.venv/bin/python"
require 'pycall'
PyCall.import_module 'urllib3'
# => <module 'urllib3' from '/opt/homebrew/opt/[email protected]/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/urllib3/__init__.py'>

# WRONG: Its using the system version of urllib3, not the version in the virtualenv

If I manually set PYTHONPATH, it will load from the virtualenv:

ENV['PYTHON'] = "/Users/seth/src/code-dot-org/.venv/bin/python"
require 'pycall'
ENV['PYTHONPATH'] = '/Users/seth/src/code-dot-org/.venv/lib/python3.12/site-packages'
PyCall.import_module 'urllib3'
# => <module 'urllib3' from '/Users/seth/src/code-dot-org/.venv/lib/python3.12/site-packages/urllib3/__init__.py'>

# CORRECT: Its using the new version of urllib from the venv

The problem with setting PYTHONPATH manually, is now I cannot invoke 3rd party python-using tools like aws cli from my process, because the manual PYTHONPATH will point them at my virtualenv.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions