Pip installs but module is not found - python

I am trying to install Exscript from github.
pip install -e 'git+git://github.com/knipknap/exscript.git#egg=Exscript'
...
Successfully installed Exscript
Cleaning up..
When i try to load it, python is not able to find it:
python2.7 -c "import Exscript"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named Exscript
But, when i try to install it in the same virtualenv with setup.py it installs and loads successfully.
What am i doing wrong?

The flag -e means "editable", and what happens behind the scenes is a symlink, and as I see, Exscript uses a directory named src, what is not seen a good practice[1].
So, in order to solve your problem, you have two alternatives:
Remove -e flag
Change Exscript to get rid of src, and use another directory name
Take a look at https://the-hitchhikers-guide-to-packaging.readthedocs.io/en/latest/creation.html#arranging-your-file-and-directory-structure and https://setuptools.readthedocs.io/en/latest/userguide/development_mode.html.
[1]: the good practice is to have the directory as the same name as used when importing the package

Related

how to install selinux package in a venv, from a self contained python installation

I have built python3.8.12 from source and installed it in /python3 on a machine with no other python anywhere. there are no "system" packages. (it is rocky8.5 linux)
Now I create and activate a venv from that python:
/python3/bin/python -m venv myvenv
source myvenv/bin/activate
Now I install packages in this venv from a local repository
pip install -r requirements.txt --index-url=file:///python3/pypkgs/simple
The repository was built with pip2pi. this is all being designed for full airgap deployments.
Now I cannot import selinux as it is apparently not aware of the selinux in the venv, and is instead searching the system.
(myvenv) [root#node1]# python -c "import selinux"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/root/myvenv/lib/python3.8/site-packages/selinux/__init__.py", line 104, in <module>
check_system_sitepackages()
File "/root/myvenv/lib/python3.8/site-packages/selinux/__init__.py", line 100, in check_system_sitepackages
raise Exception(
Exception: Failed to detect selinux python bindings at ['/python3/lib/python3.8/site-packages']
I use the selinux package as an example because it is the issue I am currently faced with. Not sure if this is a wider problem yet. Haven't tested exhaustively, but have not had issue with any other package.
Ok I guess I got it working. Not sure how horrendous of a fix this is.
First, I installed selinux to the system /python3 (apparently you can't use selinux within a venv - has to be system level?)
And then I hardcoded my /python3/bin/python to this line: https://github.com/pycontribs/selinux/blob/321d66619ed59bec00a9dc6216ce417d7befec41/selinux/__init__.py#L76

Error Python PlaySound No module named 'gi'

when I request using "playsound" library for Python to play the audio
file .mp3 it returns me the following error:
Code:
import playsound
playsound.playsound("test.mp3")
Error:
Traceback (most recent call last):
File "/home/enzoportela/PycharmProjects/SoftwareRover2018.2/SoftRover/I.A Rover(2018.2).py", line 25, in <module>
playsound.playsound("test.mp3")
File "/home/enzoportela/anaconda3/envs/SoftwareRover2018.2/lib/python3.6 /site-packages/playsound.py", line 91, in _playsoundNix
import gi
ModuleNotFoundError: No module named 'gi'
The most simple way is the vext approach.
pip install vext
pip install vext.gi
Reference:
How do I install python3-gi within virtualenv?
It seems like you are using an anaconda environment. I also faced this issue and fixed it by doing the following:
First, create a symlink to gi module in your system Python. For me, the command is as follows:
ln -s /usr/lib/python3/dist-packages/gi/ /home/USERNAME/miniconda3/lib/python3.7/site-packages/
Then, open the directory:
cd /home/USERNAME/miniconda3/lib/python3.7/site-packages/gi/
and run the following commands:
sudo cp _gi.cpython-35m-x86_64-linux-gnu.so _gi.cpython-37m-x86_64-linux-gnu.so
sudo cp _gi_cairo.cpython-35m-x86_64-linux-gnu.so _gi_cairo.cpython-37m-x86_64-linux-gnu.so
My system Python is 3.5 and miniconda Python is 3.7. Your versions may differ from mine, so take care of that in the commands above (-35m- and -37m-).
Method obtained from askubuntu

Redhat trying to use pip ImportError: No module named pip

I am trying to use pip on my Redhat system.
I installed pip following the instructions here, but when I try to use it, for example pip install, I get the following error code:
Traceback (most recent call last):
File "/usr/local/bin/pip", line 7, in ?
from pip import main
ImportError: No module named pip
this issue due to common user do not have privilege to access packages py file.
1. root user can run 'pip list'
2. other common user cannot run 'pip list'
[~]$ pip list
Traceback (most recent call last):
File "/usr/bin/pip", line 7, in <module>
from pip._internal import main
ImportError: No module named pip._internal
solution :
root user login and run
chmod -R 755 /usr/lib/python2.7
fix this issue.
If pip is already installed and your unable to access it, one of the reason could be that you don't have permissions to read or execute the library. Try doing
sudo chmod -R u+rx /usr/lib/python2.7/site-packages/pip/
If pip is installed in a different folder, you can obtain the folder path by doing
>>> import pip
>>> pip.__path__
['/usr/lib/python2.7/site-packages/pip']
If you don't have root rights and running on python 2.6 then you can try this file https://bootstrap.pypa.io/2.6/get-pip.py (it is from same instruction you used, it is a simple python script which installs all dependencies and pip itself) and run it with command python get-pip.py --user

how to make python aware of the cython module being installed in a location other than default

I had to install the Cython compiler in a location other than default. It is a academic cluster and user programs must be installed in the user's home directories. So I installed Cython with:
python setup.py install --home=~
which went fine and installed the compiler in my home /bin directory which is in my $PATH.
To test it, I launch python and do:
> from Cython.Build import cythonize
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named Cython.Build
So how to make python aware of the Cython module being installed in a location other than default?
It is strange that you use the --home parameter that way, have you tried to install via:
python setyp.py install --user
instead? This is the standard way to install python packages into your home directory. It will install everything in /home/$YOU/.local/lib/python2.7/site-packages which is appended to the pythonpath if it exist (usually it does not exist until you install something via the --user parameter).

debugging python setup.py for an open source library - bcfg2

When I install bcfg2 - http://bcfg2.org using
pip install git+git://git.mcs.anl.gov/bcfg2.git#egg=Bcfg2
or
pip install bcfg2
I am able to get bcfg2 working. Calling sudo bcfg2-admin init, shows me the set-up configuration steps/stdin prompt as expected.
When I introduce the -e option like this (in a new/clean virtualenv),
pip install -e git+git://git.mcs.anl.gov/bcfg2.git#egg=Bcfg2
And I attempt to call sudo bcfg2-admin init, I get an error message:-
calvin$ bcfg2-admin init
Traceback (most recent call last):
File "/Users/calvin/.virtualenvs/bcfg2/bin/bcfg2-admin", line 7, in <module>
execfile(__file__)
File "/Users/calvin/.virtualenvs/bcfg2/src/bcfg2/src/sbin/bcfg2-admin", line 6, in <module>
import Bcfg2.Server.Core
ImportError: No module named Bcfg2.Server.Core
This leads me to think that there is something wrong with the way setup.py is written for bcfg2. The file is located here - https://github.com/Bcfg2/bcfg2/blob/master/setup.py
Appreciate any input from pythonistas who are experts with python packaging and the configuration of setup.py.
You've installed to a virtual environment (-e). However when you run this with sudo python gets executed as root without the virtual environment available.
If you run sudo su you should be dropped into a root shell. You can then activate the virtual environment and run bcfg2-admin.
The setup.py has a bug.
Figured it out, fixed it and sent a pull request to bcfg2 maintainers.
Reference - https://github.com/Bcfg2/bcfg2/pull/28

Categories

Resources