Running bpython inside a virtualenv - python

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?

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

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.

Run bpython as module works for me
mkdir workout && cd workout
venv shell --python=3.8
Install your dependencies, e.g., pipenv install gcloud
pipenv install bpython
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
>>>

Bpython must be installed with pip3 inside each virtualenv
$ virtualenv .
$ source bin/activate
$ pip3 install bpython
$ pip3 install sqlalchemy
$ bpython
>>> import slqalchemy

Related

How to use pip3 or pip in pyenv for virtual python I created?

1)I use multi -versions,but I when I use pyenv to shift to other version(installed by pyenv),I cant install any package like numpy and tensorflow.
lzw#resplendent-star:~$ python3
Python 3.8.1 (default, Apr 11 2020, 17:03:40)
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'
>>> exit()
lzw#resplendent-star:~$ sudo -H pip3 install numpy
Requirement already satisfied: numpy in /usr/lib/python3/dist-packages
you could see,3.8.1 is what I installed by pyenv,but I cant install numpy for it.
2)The worst thing,how could I use system default python?
You should make sure that you are using the correct pip to install your dependencies.
The quickest way is usually to try:
python3 -m pip install numpy
This makes sure that you are using the pip corresponding to the version of python that you want to use. However this method sometimes does not work on some Linux distributions that remove the built-in pip.
First of all, check that pyenv is installed correctly. The executables of python and pip should point to the pyenv shims:
$ type python
python is /home/tyrion/.local/share/pyenv/shims/python
$ type pip
pip is /home/tyrion/.local/share/pyenv/shims/pip
Then, the usual workflow with pyenv is the following:
List the python versions installed on your system
$ pyenv versions
* system (set by /home/tyrion/.local/share/pyenv/version)
3.6.8
3.8.1
other_version
Activate the Python version that you want to use, with pyenv shell, pyenv local or pyenv global:
$ pyenv shell 3.8.1
Check that the Python and Pip executable are what we expect (same version of python):
$ pyenv which python
/home/tyrion/.local/share/pyenv/versions/3.8.1/bin/python
$ pyenv which pip
/home/tyrion/.local/share/pyenv/versions/3.8.1/bin/pip
Install your dependency
pip install numpy
Note that if you installed pyenv in your home folder, you do not need to use sudo!
Use your library
$ python
>>> import numpy

Tensorflow Update Forcing Python3.5 when 3.7 installed

I currently have Tensorflow 1.14.0 installed. I upgraded Python to 3.7 in order to get at least Tensorflow 1.15 if not 2.0. My python updates were successful:
pi#HCoreDEV:~ $ python --version
Python 3.7.0
pi#HCoreDEV:~ $ python3 --version
Python 3.7.0
Now, when I try
sudo pip install --upgrade tensorflow
I get the following (there are a LOT more lines, but they all say "requirement already satisfied" or "up to date"):
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already up-to-date: tensorflow in /usr/local/lib/python3.5/dist-packages (1.14.0)
NOTE that it defaults to the python 3.5 folder.... ????
BUT: If I try to check the VERSION of TF:
pi#HCoreDEV:~ $ python3 -c 'import tensorflow as tf; print(tf.__version__)'
Traceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'tensorflow'
pi#HCoreDEV:~ $ python -c 'import tensorflow as tf; print(tf.__version__)'
Traceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'tensorflow'
It doesn't even see ANY TF installed.
Here is my update-alternatives output:
pi#HCoreDEV:~ $ sudo update-alternatives --config python
There are 3 choices for the alternative python (providing /usr/bin/python).
Selection Path Priority Status
------------------------------------------------------------
0 /usr/bin/python3.7 3 auto mode
1 /usr/bin/python2.7 1 manual mode
2 /usr/bin/python3.5 2 manual mode
* 3 /usr/bin/python3.7 3 manual mode
Press <enter> to keep the current choice[*], or type selection number:
And finally, here is my sourced ~/.bashrc file's aliases for python and python3 for good measure:
alias python='/usr/bin/python3.7'
alias python3='/usr/bin/python3.7'
So WHY is it that with confirmed python3.7 installed and running, Tensorflow only looks at the 3.5 folder, sees an existing install that was running before I upgraded to 3.7, and says the requirements are satisfied?
When I just type "python" into my terminal window, this is the output:
pi#HCoreDEV:~ $ python
Python 3.7.0 (default, Feb 6 2020, 16:11:11)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
Lastly, here is my sys.path output:
>>> import sys
>>> sys.path
['', '/usr/local/lib/python37.zip', '/usr/local/lib/python3.7', '/usr/local/lib/python3.7/lib-dynload', '/usr/local/lib/python3.7/site-packages']
>>>
Need some help, I hope I've provided enough info to make the issue obvious to someone with a bit more experience than me.
The latest Python version on Stretch is 3.5. The command line tool pip calls the Python 3.5 Pip module. If you have another version installed and you want to install/update packages for that specific version, then you must use the correct Pip by running it like this (assuming python3 is the correct version or an alias):
$ python3 -m pip install --upgrade tensorflow
Or if you need sudo:
$ sudo -H python3 -m pip install --upgrade tensorflow
If you don't have Pip installed for that specific Python version you can run:
$ sudo -H python3 -m ensurepip

Pip3 for Python3 in virtual environment not installing package according to path

I am trying to install packages in a Python3 virtual environment using pip3. Pip3 is pointing to the virtual environment when it is active.
Packages will not install, since they are installed outside of the virtual environment, already satisfied outside of the virtual environment.
How do I install packages in the virtual environment?
And how can I be sure that I am actually using the interpreter inside the virtual environment? When pip3 is not acting according to the location returned by 'which', I do can not be sure that the python I am using in fact is the one from the virtual environment.
(venv_name) user_name#user_name-ubuntu:/mnt/ramdisk$ which python3
/mnt/ramdisk/venv_name/bin/python3
(venv_name) user_name#name-ubuntu:/mnt/ramdisk$ which pip3
/mnt/ramdisk/venv_name/bin/pip3
(venv_name) user_name#name-ubuntu:/mnt/ramdisk$ sudo pip3 install pandas
Requirement already satisfied: pandas in /usr/local/lib/python3.6/dist-packages ...
(venv_name) user_name#name-ubuntu:/mnt/ramdisk$ python3
Python 3.7.5 (default, Oct 15 2019, 22:40:07)
[GCC 7.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import pandas
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'pandas'
OS: UBUNTU 18.04
Solved by not using sudo. Sudo was used for access.

How do I install python3-gi within virtualenv?

I'm following the Python GTK+ 3 Tutorial and I'm trying to get a working install running in virtualenv. I have python3-gi installed through the Ubuntu package manager already. Things look like this:
:~$ mkvirtualenv py3 --python=/usr/bin/python3
Running virtualenv with interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in py3/bin/python3
Also creating executable in py3/bin/python
Installing setuptools, pip...python
done.
(py3):~$ python
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gi
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'gi'
>>>
(py3):~$ deactivate
:~$ /usr/bin/python3
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gi
>>>
As you can see, python3-gi is obviously not available within virtualenv but I am not sure how to install it since python3-gi is installed through my package manager and not with pip.
It is now possible to resolve this using vext. Vext allows you to install packages in a virtualenv that individually access your system packages. To access gi, do the following:
pip install vext
pip install vext.gi
Update 2018 – Debian Stretch
Install GTK+ 3 / GIR.
apt install libcairo2-dev libgirepository1.0-dev gir1.2-gtk-3.0
Create a virtual environment.
python3 -mvenv venv
Install pygobject (pycairo should come as a dependency).
venv/bin/pip install pygobject
Update 2018 – macOS
Install GTK+ 3 and Gobject Introspection with Homebrew.
brew install gtk+3 gobject-introspection
Create and activate a virtual environment.
python3 -mvenv venv
Install pygobject (pycairo should come as a dependency).
PKG_CONFIG_PATH=/usr/local/opt/libffi/lib/pkgconfig ARCHFLAGS="-arch x86_64" venv/bin/pip install pygobject
Original answer
This is what I did to get GTK+ 3 within a Python 3.5 virtual environment on OS X 10.11.
Install GTK+ 3 with Homebrew.
brew install gtk+3
Create and activate a virtual environment.
pyvenv-3.5 venv
source venv/bin/activate
cd venv
Install pycairo on the virtual environment.
export PKG_CONFIG_PATH=$VIRTUAL_ENV/lib/pkgconfig
curl -L https://cairographics.org/releases/pycairo-1.10.0.tar.bz2 | tar xj
cd pycairo-1.10.0
export ARCHFLAGS='-arch x86_64'
python waf configure --prefix=$VIRTUAL_ENV # It's ok, this will fail.
sed -i '' '154s/data={}/return/' .waf3-1.6.4-*/waflib/Build.py # Bugfix: https://bugs.freedesktop.org/show_bug.cgi?id=76759
python waf configure --prefix=$VIRTUAL_ENV # Now it should configure.
python waf build
python waf install
unset ARCHFLAGS
cd ..
Install pygobject on the virtual environment.
export PKG_CONFIG_PATH=$VIRTUAL_ENV/lib/pkgconfig:/usr/local/opt/libffi/lib/pkgconfig
curl -L http://ftp.gnome.org/pub/GNOME/sources/pygobject/3.12/pygobject-3.12.2.tar.xz | tar xJ
cd pygobject-3.12.2
./configure CFLAGS="-I$VIRTUAL_ENV/include" --prefix=$VIRTUAL_ENV
make
make install
cd ..
Profit.
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 5 2015, 21:12:44)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from gi.repository import Gtk, Gdk, Pango, GObject
>>> from cairo import ImageSurface, Context, FORMAT_ARGB32
>>>
Python 3.5 downloaded and installed from PSF.
I haven't found a proper solution to this. When I run into situations where I can't get something to install into a virtualenv directly, I symlink it there and it works fine (there are probably exceptions, but this is not one of them).
ln -s /usr/lib/python3/dist-packages/gi /path_to_venv/lib/python3.4/site-packages/
Not elegant in the slightest; seems nicer than giving the virtualenv full access to all system packages though (via --system-site-packages).
The pip package name is somewhat counterintuitive - use pip install PyGObject.
I installed pgi via pip, which may be an option. It is apparently API compatible with PyGObject and so far seems to work ok running Gtk.
On Ubuntu (tested in 19.04), you can download the package and then install it as follows:
apt-get -y download python3-gi
dpkg-deb -x <package>.deb <virtualenv path>
In Ubuntu, the name of the downloaded package is python3-gi_3.32.0-1_amd64.deb.

Installation of nltk-3.0a3 for python3.3.3 - "ImportError: No module named 'setuptools'"

I have installed Python 3.3.3 with a command:
sudo apt-get install python3
I am calling it as Python3, because I have Python 2.7 installed as well.
I have also installed distribute (setuptools) with a command:
sudo apt-get install python3-setuptools
To be sure, setuptools are installed for python3, I have searched with pip3 and I received an output:
$ pip3 search setuptools
setuptools - Easily download, build, install, upgrade, and
uninstall Python packages
INSTALLED: 2.0 (latest)
Now I want to install NLTK's version for Python 3, so nltk3.0a3. I have downloaded it from the official site, and in the folder I ran a standard python installation and received an output:
$ python3 setup.py install
Traceback (most recent call last):
File "setup.py", line 33, in <module>
from setuptools import setup, find_packages
ImportError: No module named 'setuptools'
And in fact, when I go to python3 command line, for that input, I see the output:
>>> help('modules setup')
ez_setup - Bootstrap setuptools installation
setup
unittest.test.test_setups
So there is really no module called setuptools...
Does anyone know what am I doing wrong? Or is there any other way of installing NLTK for python 3? (in apt, there is only NLTK for python 2).
Thanks a lot guys for any effort!
You can use muon or your favorite package manager to install python3-setuptools and an experimental version of nltk supporting Python 3 is available at http://nltk.org/nltk3-alpha/
I suspect you will also need pyyaml and numpy installed for your python 3.
I gave this a try:
muon
check that I have installed python3-setuptools and python3-numpy
sudo pip3 install pyyaml
Having downloaded and unpacked the code from the above link
cd ~/ToolBuild/nltk-3.0a3/nltk-3.0a3
sudo python3 setup.py install
python3
Python 3.3.2+ (default, Oct 9 2013, 14:50:09)
[GCC 4.8.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import nltk
>>> dir(nltk)
['AbstractLazySequence', 'AffixTagger', 'AlignedSent', 'Alignment', 'AnnotationTask', 'Assignment', 'BigramAssocMeasures', 'BigramCollocationFinder', 'BigramTagger', 'BinaryMaxentFeatureEncoding', 'BlanklineTokenizer', 'BottomUpChartParser', 'BottomUpLeftCornerChartParser', 'BottomUpProbabilisticChartParser', 'Boxer', 'BrillTagger', 'BrillTaggerTrainer', 'CfgReadingCommand', 'ChartParser', 'ChunkParserI',..........

Categories

Resources