import module fail when using sudo - python

I would like to import pylibfreenect2, when I am in user mode, it work fine.
python3
>>> import pylibfreenect2
However, It fail import module in sudo and sudo -E -H
sudo python3
>>> import pylibfreenect2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/user/.local/lib/python3.5/site-packages/pylibfreenect2/__init__.py", line 108, in <module>
from .libfreenect2 import *
ImportError: libfreenect2.so.0.2: cannot open shared object file: No such file or directory
Because I need to use python2.7 sometimes, I cannot use PYTHONPATH in ~/.bashrc.
Is there other method to solve? Thank you very much, My OS is ubuntu.

You can use pyenv to manage different Python versions and switch between them.

The command:
sudo -E -H
Does not save the current HOME variable, when using -H you need to indicate the target user (cf. https://www.sudo.ws/man/1.8.3/sudo.man.html). Try to use:
sudo -E -H -u ${your_user}

Related

Symbol not found: _FSPathMakeRef

Can't import Quartz package.
I have installed it with this command pip install pyobjc-framework-Quartz. Tried reinstalling python, also tried python -m pip install .... With python2 or sudo python3, everything works fine but python3 is giving me this error message every time I try importing Quartz
Python version - 3.10.4
Mac version - Big Sur 11.6.5
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import Quartz
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/Quartz/__init__.py", line 6, in <module>
import AppKit
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/AppKit/__init__.py", line 10, in <module>
import Foundation
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/Foundation/__init__.py", line 9, in <module>
import CoreFoundation
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/CoreFoundation/__init__.py", line 9, in <module>
import objc
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/objc/__init__.py", line 6, in <module>
from . import _objc
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/objc/_objc.cpython-310-darwin.so, 2): Symbol not found: _FSPathMakeRef
Referenced from: /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/objc/_objc.cpython-310-darwin.so
Expected in: flat namespace
in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/objc/_objc.cpython-310-darwin.so
For investigation purposes, can you try :
cd /tmp
python3 -m venv venv
source venv/bin/activate
pip install pyobjc-framework-Quartz
python your-script.py
Can you try this to see if it works :
env -i /Library/Frameworks/Python.framework/Versions/3.10/bin/python3 my_script.py
You may have files only accessible by root, try to change ownership:
sudo chown -R $(id -u):$(id -g) /Library/Frameworks/Python.framework/Versions/3.10
and run env -i ... again.
If you run :
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3 -c 'import sys;print(sys.path)'
sudo /Library/Frameworks/Python.framework/Versions/3.10/bin/python3 -c 'import sys;print(sys.path)'
Is there any difference between the two ?
Try following to see if it improves :
sudo /Library/Frameworks/Python.framework/Versions/3.10/bin/python3 -m pip install pyobjc-framework-Quartz
Try this to see if there is anything unusual :
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3 -X importtest -v -c 'import Quartz'
You might need to try:
python3 -m pip install [...]
Hope this will hope.

ImportError: cannot import name '_gi' from partially initialized module 'gi' (most likely due to a circular import)

Looks like I have broken my python installation when I wanted to switch to python 3.8. Using Ubuntu 18.04. Trying to use the gi, gives the following error:
$ python
Python 3.8.1 (default, Dec 31 2019, 18:42:42)
[GCC 7.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from gi.repository import GLib, Gio
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3/dist-packages/gi/__init__.py", line 42, in <module>
from . import _gi
ImportError: cannot import name '_gi' from partially initialized module 'gi' (most likely due to a circular import) (/usr/lib/python3/dist-packages/gi/__init__.py)
Tried running update-alternatives for python, but it tells me there is only one python alternative configured (3.8).
Tried to reinstall python3-gi and python3.8. Still the same problem
I had the same issue. I linked python3 to python3.6, for me it was pointing to 3.8. That solved the issue.
cd /usr/bin/
rm python3
ln -s python3.6 python3
Thats all. Now my system started working fine.
Install gi for python 3.8: python3.8 -m pip install pgi
Then, instead of import gi use:
import pgi
pgi.install_as_gi()
from gi.repository import GLib, Gio
Alternatively, you can force install PyGObject for python 3.8:
sudo python3.8 -m pip install --ignore-installed PyGObject
which should allow one to from gi import ... as before.
For me the workaround was to create a symlink:
cd /usr/lib/python3/dist-packages/gi/
sudo ln -s _gi.so _gi.cpython-38-x86_64-linux-gnu.so
and it solved the problem for me.
I had the same problem on ubuntu 18 as python3 was referring to python3.9.
In order to solve it, I changed the alternative for python3:
sudo update-alternatives --config python3
There are 2 choices for the alternative python3 (providing /usr/bin/python3).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/python3.9 2 auto mode
1 /usr/bin/python3.6 1 manual mode
2 /usr/bin/python3.9 2 manual mode
By choosing number 1, now python3 points to python3.6 and everything works fine again
Found answer here https://bugzilla.redhat.com/show_bug.cgi?id=1709787:
The cause is - /usr/lib64/python3.8/site-packages/gi/_gi.cpython-38m-x86_64-linux-gnu.so has incorrect name:
sh-5.0# python3 -c 'from gi.repository import GLib'
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib64/python3.8/site-packages/gi/init.py", line 42, in
from . import _gi
ImportError: cannot import name '_gi' from 'gi' (/usr/lib64/python3.8/site-packages/gi/init.py)
sh-5.0# mv /usr/lib64/python3.8/site-packages/gi/_gi.cpython-38m-x86_64-linux-gnu.so /usr/lib64/python3.8/site-packages/gi/_gi.cpython-38-x86_64-linux-gnu.so
sh-5.0# python3 -c 'from gi.repository import GLib'
Note that since 3.8.0a4, the "m" is not supposed to be there. Is it somehow hardcoded?
sh-5.0# python3-config --extension-suffix
.cpython-38-x86_64-linux-gnu.so
in my case it was
$ sudo ln -s /usr/lib/python3/dist-packages/gi/_gi.cpython-35m-x86_64-linux-gnu.so /usr/lib/python3/dist-packages/gi/_gi.cpython-38-x86_64-linux-gnu.so
$ sudo ln -s /usr/lib/python3/dist-packages/gi/_gi_cairo.cpython-35m-x86_64-linux-gnu.so /usr/lib/python3/dist-packages/gi/_gi_cairo.cpython-38-x86_64-linux-gnu.so
The reason of this error is that this app can't find the matched Python version of _gi_cairo.cpython-(version)-x86_64-linux-gnu.so.
Normally, this unmatched situation is caused by some wrong mixed usage of different versions of Python.
So basically, you can try to switch your Python version ( to the default version of your OS). Or you can go to '/usr/lib/python3/dist-packages/gi' and create a new .so library file:
cp _gi_cairo.cpython-(old version)-x86_64-linux-gnu.so _gi_cairo.cpython-(new version)-x86_64-linux-gnu.so
or
ln -s _gi_cairo.cpython-(old version)-x86_64-linux-gnu.so _gi.so
Have same issue, can't load module from script folder. In my case work if i changing workdir for example (but only for inline command):
cd /tmp; python -c 'from gocd import Server'
echo $?
0
After copy script to /tmp, it's does't work
cp gocd.py /tmp
cd /tmp; python -c 'from gocd import Server'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/gocd.py", line 3, in <module>
from gocd import Server
ImportError: cannot import name 'Server' from partially initialized module 'gocd' (most likely due to a circular import) (/tmp/gocd.py)

ImportError: No module named 'gammu'

I work on Ubuntu Xenial (16.04) with python3, I also installed anaconda.
I installed python3-gammu (with apt install python3-gammu or/and pip install python3-gammu) to test send SMS.
Just run python3 console and
>>> import gammu
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'gammu'
import sys
print(sys.path)
only return anaconda paths !
If I run
sudo find -iname gammu
…
./usr/lib/python3/dist-packages/gam
…
so if I add this path:
>>> sys.path.append('/usr/lib/python3/dist-packages/')
>>> import gammu
and it works !
Could you clarify this library path issue?
just
export PYTHONPATH=$PYTHONPATH:/usr/lib/python3/dist-packages/
To keep it at next reboot, put this line in your ~/.bashrc :
# added by Anaconda3 4.2.0 installer
export PATH="/home/my_user_name/anaconda3/bin:$PATH"
export PYTHONPATH="/usr/lib/python3/dist-packages/:$PYTHONPATH"
to active new .bashrc, do not forget to run
source ~/.bashrc
When you are trying to import any package it will check sys.path, which contains all paths of packages. If it find the package you want to import it will import it.
sorry for bad english...
Why use sys.path.append(path) instead of sys.path.insert(1, path)?
You may get clarity after seeing this ?

python virtualenv module import error

I am using virtualenv and have activated it and tested it like this:
source .virtualenvs/myapp/bin/activate
pip freeze
(myapp) me: redis==2.05
Then checking redis can import OK:
(myapp) me: python
>>>import redis
This works OK.
However on running the following
(myapp) me: sudo ./manage.py database create
I get the following error:
Traceback (most recent call last):
File "./manage.py", line 4, in <module>
from myapp import manager
File "/home/me/myapp/__init__.py", line 1, in <module>
import redis
ImportError: No module named redis
Redis is clearly installed for the virtualenv, any thoughts on what could be going wrong?
I suspect that sudo is the reason. It does not properly preserve the virtualenv.
Just try to create a shell script, which first sources the environment and than executes python. Than run this shell script with the sudo command:
#!/bin/sh
# ensure that working directory is right
source .virtualenvs/myapp/bin/activate
./manage.py database create
and run it:
$ chmod ugo+x my-startup-shell.sh
$ sudo my-startup-shell.sh
If that works, do some experiments with sudo like possibly passing the -E option to inherit the environment etc.

Python wx via Homebrew

I'm having issues install Python wx on my Mac. Here's what I did:
brew install wxpython
which gave this Caveat:
Caveats
Python modules have been installed and Homebrew's site-packages is not
in your Python sys.path, so you will not be able to import the modules
this formula installed. If you plan to develop with these modules,
please run:
mkdir -p /Users/snowcrash/Library/Python/2.7/lib/python/site-packages
echo 'import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")'
>> /Users/snowcrash/Library/Python/2.7/lib/python/site-packages/homebrew.pth
So I ran the 2 commands from the Terminal as suggested:
mkdir -p /Users/snowcrash/Library/Python/2.7/lib/python/site-packages
>> echo 'import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")'
but when I try to import wx I am getting:
>>> import wx
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/wx-3.0-osx_cocoa/wx/__init__.py", line 45, in <module>
from wx._core import *
File "/usr/local/lib/python2.7/site-packages/wx-3.0-osx_cocoa/wx/_core.py", line 4, in <module>
import _core_
ImportError: dlopen(/usr/local/lib/python2.7/site-packages/wx-3.0-osx_cocoa/wx/_core_.so, 2): no suitable image found. Did find:
/usr/local/lib/python2.7/site-packages/wx-3.0-osx_cocoa/wx/_core_.so: mach-o, but wrong architecture
and this doesn't seem to help:
How do I install wxPython on Mac OS X?
Any suggestions?
Turned out I had to do this:
defaults write com.apple.versioner.python Prefer-32-Bit -bool no

Categories