How do I install python3-gi within virtualenv? - python

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.

Related

cannot install library python

i want to install the library telethon
It says i should execute this line of code
python3 -m pip install --upgrade pip
python3 -m pip install --upgrade telethon
The first command works, it has a bunch of warnings but it seems to work.
But the second command says:
ERROR: Could not find a version that satisfies the requirement telethon (from versions: none) ERROR: No matching distribution found for telethon
So what i am doing wrong?
I am using windows
To install a virtual env and your package:
$ python --version
Python 3.6.9
$ python -m venv venv
$ source venv/bin/activate
(venv)$ pip install -U pip setuptools
(venv)$ pip install telethon
(venv)$ python
Python 3.6.9 (default, Jun 13 2020, 20:27:45)
[GCC 4.2.1 Compatible Apple LLVM 11.0.3 (clang-1103.0.32.62)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Now try to load telethon:
>>> import telethon
>>> telethon.__version__
'1.23.0'
I have found the solution:
I just have to run VS code editor as "admin"

Can't setup Python venv on new Mac

I have miniconda installed on my new Mac at:
/opt/miniconda3/bin/python
My .zsh terminal shows the default Python as 2.7:
Python 2.7.16 (default, Dec 21 2020, 23:00:36)
[GCC Apple LLVM 12.0.0 (clang-1200.0.30.4) [+internal-os, ptrauth-isa=sign+stri on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
I am trying to create a venv for a project I want to work on in VSCode. I am navigating to the folder and typing:
20:38:54:~/Documents/Python_Projects/pword_proj % pip3 install virtualenv
and I get this error:
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
I also tried:
20:39:40:~/Documents/Python_Projects/pword_proj % pip install virtualenv
and I get this error:
zsh: command not found: pip
I am not sure what I am doing incorrect here. Thanks
Python version 2.7 has been depreciated and so pip3 is now being used. You can try installing virtualenv with brew:
brew install pyenv-virtualenv
You can install home-brew here https://brew.sh
You may also need to reinstall the CommandLineTools using:
sudo rm -rf /Library/Developer/CommandLineTools
xcode-select --install
If you already have miniconda installed, I do not believe you need virtualenv since miniconda allows you to use conda environments. Conda does what virtualenv can do and more.
You can learn more about managing conda environments here. To create a conda environment, you can use the following command.
conda create --name myenv
That being said, it seems that your shell is not recognizing the pip command. This may be due to not being in your base conda environment, so enable that by using
source activate
If pip still isn't found after that, I would recommend checking your environment variables under PATH.

python3: /lib/x86_64-linux-gnu/libm.so.6: version `GLIBC_2.29' not found (required by python3)

When I use :
$ source ./bin/activate
(wink) mohsen#debian:~/m_and_m/wink$ python3
python3: /lib/x86_64-linux-gnu/libm.so.6: version `GLIBC_2.29' not found (required by python3)
python3: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.28' not found (required by python3)
python3: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.25' not found (required by python3)
python3: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.26' not found (required by python3)
And When :
$ deactivate
mohsen#debian:~/m_and_m/wink$ python3
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
Indeed it can't load glibc or its path when I use virtualenv.
How can I solve it?
You have to recreate your virtual environment. You can follow the instructions from pythonanywhere with some adaptations:
If not already done, manually record packages you are using in a requirements.txt file. You cannot use pip freeze as in the pythonanywhere's instructions because the original virtualenv is broken.
Remove your old virtualenv (make a backup before)
rm -rf /home/myusername/path/to/virtualenv
Create a new virtualenv
virtualenv --python=pythonX.Y /home/myusername/path/to/virtualenv
Reinstall your packages
pip install -r requirements.txt

python and ipython pointing towards different installations after brew update and upgrade

I am on macOS Sierra 10.12.6
Today I run
brew update
brew upgrade
It upgraded a bunch of packages and installations including python. After the upgrade, while ipython points towards the right installation, python does not. For example:
when I type:
$ which python
/usr/bin/python
so that:
$ python
Python 2.7.10 (default, Feb 7 2017, 00:08:15)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
but:
$ which ipython
/usr/local/bin/ipython
so that
$ ipython
Python 2.7.13 (default, Aug 17 2017, 16:42:26)
Type "copyright", "credits" or "license" for more information.
or:
$ which pip
/usr/local/bin/pip
which is also fine.
of course, my .bash_profile includes:
# Homebrew
export PATH=/usr/local/bin:$PATH
and:
$ echo $PATH
/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin
After running brew upgrade I installed today also the command line tools with:
xcode-select --install
since it was recommended during the upgrade, but I doubt this has anything to do with the problem (?)
I have tried
$ brew link python
Warning: Already linked: /usr/local/Cellar/python/2.7.13_1
To relink: brew unlink python && brew link python
and then brew unlink python && brew link python
but the problem remains.
To summarise: how can I make the installation consistent? or How can I get python to point to the correct installation?
EDIT:
During the brew upgrade it installed both, python2 and python3. To access to these installations you need to specify the version, so:
$ which python
/usr/bin/python
$ which python2
/usr/local/bin/python2
$ which python3
/usr/local/bin/python3
and note that I did not have python3 before running brew upgrade.
The solution can be found by calling:
brew info python
then one can read
==> Caveats
This formula installs a python2 executable to /usr/local/bin.
If you wish to have this formula's python executable in your PATH then add
the following to ~/.bash_profile:
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
so simply add:
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
to the ~/.bash_profile file
And this solves the problem.

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