20

I have created a virtualenv and installed SQLAlchemy in it:

$ virtualenv alchemy
$ source alchemy/bin/activate
$ pip install sqlalchemy

import works in python:

$ python
Python 2.7.5 (default, Mar 9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlalchemy
>>> print sqlalchemy.__version__
0.9.7

But it does not work in bpython:

>>> import sqlalchemy
Traceback (most recent call last):
 File "<input>", line 1, in <module>
ImportError: No module named sqlalchemy

Why can't bpython find the package installed in the virtualenv, even though it is executed after source alchemy/bin/activate is called?

asked Aug 21, 2014 at 19:37

5 Answers 5

33

bpython must be installed in the virtualenv, otherwise the external, system-wide bpython is called:

$ source alchemy/bin/activate
(alchemy)[ 10:34PM ] [ adamatan@rubidium:/tmp ]
$ pip install bpython
...
$ alchemy/bin/bpython
--------------
>>> import sqlalchemy
>>> print sqlalchemy.__version__
0.9.7
answered Aug 21, 2014 at 19:37
Sign up to request clarification or add additional context in comments.

3 Comments

Is there a way to avoid this behaviour and access the /usr/lib bpython with the virtualenv?
Yes there is. You need to resource your activate script : $ source alchemy/bin/activate $ which bpython /usr/bin/bpython $ source alchemy/bin/activate $ which bpython alchemy/bin/bpython
If you leave and re-enter the venv after installing then the correct bpython will be found. No need to give the path to venv's bin dir any more.
9

bpython has the python it was installed with hardcoded in its shebang.

You can manually edit it to make it use the current python. Open the script by running for instance $ vi $(which bpython).

Then change the top line from eg. #!/usr/bin/python3 to eg. #!/usr/bin/env python3.

That should make it run using the venv's python. It's not supported officially by bpython but it has always worked for me on both Mac OS X and Ubuntu.

answered May 30, 2016 at 21:36

Comments

2

Bpython must be installed with pip3 inside each virtualenv

$ virtualenv . 
$ source bin/activate 
$ pip3 install bpython
$ pip3 install sqlalchemy
$ bpython

>>> import slqalchemy

answered Dec 11, 2020 at 9:20

Comments

2

Run bpython as module works for me

  1. mkdir workout && cd workout
  2. venv shell --python=3.8
  3. Install your dependencies, e.g., pipenv install gcloud
  4. pipenv install bpython
  5. python -m bpython
python -m bpython
bpython version 0.21 on top of Python 3.8.5 /home/dennys/.local/share/virtualenvs/workout-qTtsVfjR/bin/python
>>> from gcloud import bigquery
>>> 
answered Mar 13, 2021 at 18:20

Comments

0

If bpython does not exists in your venv bin folder you can find it with sudo find / -name bpython. In my case it got installed in ~/.local/bin/

$ source alchemy/bin/activate
$ pip install bpython
...
$ ~/.local/bin/bpython
answered Jul 27, 2023 at 9:02

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.