astral-sh / rye

a Hassle-Free Python Experience
https://rye.astral.sh
MIT License
13.6k stars 466 forks source link

IPython not working with Rye `.venv` #1247

Closed jamesbraza closed 2 months ago

jamesbraza commented 2 months ago

Steps to Reproduce

It seems IPython doesn't work with a .venv made by rye sync:

> rye sync
Reusing already existing virtualenv
Generating production lockfile: /path/to/repo/requirements.lock
Generating dev lockfile: /path/to/repo/requirements-dev.lock
Installing dependencies
Resolved 65 packages in 540ms
   Built pqapi @ file:///path/to/repo
Prepared 1 package in 526ms
Uninstalled 1 package in 0.53ms
Installed 1 package in 1ms
 - pqapi==6.3.1.dev10+g13987e0.d20240720 (from file:///path/to/repo)
 + pqapi==6.3.1.dev10+g13987e0.d20240720 (from file:///path/to/repo)
Done!
>  source .venv/bin/activate
> which python
/path/to/repo/.venv/bin/python
> pip list
Package           Version
----------------- -------
asttokens         2.4.1
decorator         5.1.1
executing         2.0.1
ipython           8.26.0
jedi              0.19.1
matplotlib-inline 0.1.7
parso             0.8.4
pexpect           4.9.0
pip               24.0
prompt_toolkit    3.0.47
ptyprocess        0.7.0
pure-eval         0.2.2
Pygments          2.18.0
six               1.16.0
stack-data        0.6.3
traitlets         5.14.3
wcwidth           0.2.13
>  ipython
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'IPython'
> python -m ipython
/path/to/repo/.venv/bin/python: No module named ipython

Expected Result

IPython to work in a Rye .venv

Actual Result

ipython Traceback (most recent call last): File "", line 1, in ModuleNotFoundError: No module named 'IPython'

Version Info

rye 0.36.0 commit: 0.36.0 (2024-07-07) platform: macos (aarch64) self-python: cpython@3.12.3 symlink support: true uv enabled: true

Stacktrace

No response

bluss commented 2 months ago

Do you maybe have the complete steps to reproduce here? (From rye init, rye add etc or complete pyproject.toml). Is IPython installed in the current rye project or not?

If ipython is installed into the local rye project, the default way to run it is rye run ipython python -m IPython will also work (using rye's python shim).

jamesbraza commented 2 months ago

Yeah here is a full repro:

> rye init project
> cd project
> rye lock
> source .venv/bin/activate
> pip install ipython
> ipython
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'IPython'

I realized the issue is that rye-made .venv's don't seem to work with pip:

> which python
/Users/user/code/project/.venv/bin/python
> which pip
pip: aliased to noglob pip3
 > which pip3
/Users/user/.pyenv/shims/pip3

Do you know why pip is not properly aliased to the .venv when it's activated?

charliermarsh commented 2 months ago

They don't include a user-exposed pip at all. It's part of Rye's philosophy: https://rye.astral.sh/philosophy/.

jamesbraza commented 2 months ago

Oh I see, sorry for the false report here. Good to know and appreciate the response!

bluss commented 2 months ago

Great that it was solved. Ipython has a feature where it can interface with venvs it's not installed in, so for some typical use cases I would suggest doing that (however, it needs to be the same python version between the ipython install and the venv).

jamesbraza commented 2 months ago

Thanks @bluss , appreciate the follow up. Actually if you have a second, I am curious about this:

Ipython has a feature where it can interface with venvs it's not installed in

I just set up something like:

  1. pyenv global 3.12.4, with ipython installed
  2. rye local with Python 3.12.4, no ipython installed/added, rye's .venv activated

Do you know how to trigger that IPython feature such that it falls back onto what's installed in system Python?

bluss commented 2 months ago

The recipe is like this

Activate the venv that has the libraries you want to import (the 2). Run ipython using the python where it's installed. That's /path/to/ipython/environment/bin/ipython (/path/to/ipython/environment/bin/python -m IPython should also be an alternative)

jamesbraza commented 2 months ago

Ah I follow and thank you. So in my case then what I'd do is:

source .venv/bin/activate
/Users/user/.pyenv/shims/python -m IPython

Case closed, cheers!