ImportError: cannot import name for Python package - python

I am using Python 3.6 and I have a strange import error. My Python file contains only the line import formic. But when I execute the script, I get the following error:
from formic import FileSet, Pattern, get_version
ImportError: cannot import name 'FileSet'
I have tried installing Formic both with and without sudo (yes, I know that I shouldn't use pip with sudo, but sometimes you get desperate)
I have used Formic before, but can't figure out what is going on in this situation.
If I only have import sys, then the script runs just fine with no errors, as expected.
Any ideas why this error occurs? Or how to fix it?
Additional Information
I don't have a circular dependency, because Formic is a Python package
https://stackoverflow.com/a/9252628/623541
I have deleted the __pycache__ folder
https://stackoverflow.com/a/73954626/623541
I have tried fixing PYTHONPATH
https://stackoverflow.com/a/15052360/623541
export PYTHONPATH=/home/myName/Workspace/myProject/.pyenv/lib/python3.6/site-packages
I have tried installing with sudo -H
https://stackoverflow.com/a/52159621/623541
I have tried installing Formic with --no-cache-dir
https://stackoverflow.com/a/9510610/623541
I have purged cache with pip cache purge
I have tried fixing permissions in the site-packages directory.
https://stackoverflow.com/a/16377297/623541
I have verified that Formic is installed:
$ /home/myName/Workspace/myProject/.pyenv/bin/pip3 show formic
Name: formic
Version: 0.9b8
Summary: An implementation of Apache Ant FileSet and Globs
Home-page: http://www.aviser.asia/formic
Author: Aviser LLP, Singapore
Author-email: formic#aviser.asia
License: GPLv3+
Location: /home/myName/Workspace/myProject/.pyenv/lib/python3.6/site-packages
Requires:
Required-by:
But if I try to import Formic from the same Python, it fails:
$ /home/myName/Workspace/myProject/.pyenv/bin/python
Python 3.6.9 (default, Jun 29 2022, 11:45:57)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import formic
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/myName/Workspace/myProject/.pyenv/lib/python3.6/site-packages/formic/__init__.py", line 27, in <module>
from formic import FileSet, Pattern, get_version
ImportError: cannot import name 'FileSet'

The only solution I found was to upgrade to Formic2 and update the code to support it.

Related

Python 3.11 installation: some modules are missing

I'll try to be as clear and specific as possible because I've seen so many people with the same problem, but none of the answers they had worked for me.
The problem in question is: missing modules when trying to import other modules, that (by far as I understand) are installed by default in Python 3.11. eg: missing _sqlite3 when importing sqlite3.
Python 3.11.0 (main, Jan 12 2023, 03:19:52) [GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/user_name/opt/Python-3.11.0/lib/python3.11/sqlite3/__init__.py",
line 57, in <module> from sqlite3.dbapi2 import *
File "/home/user_name/opt/Python-3.11.0/lib/python3.11/sqlite3/dbapi2.py",
line 27, in <module> from _sqlite3 import *
ModuleNotFoundError: No module named '_sqlite3'
Context
I'm currently working on a Dreamhost VPS, running on Ubuntu 20.04.5. Since it's a Dreamhost VPS keep in mind I can't uso sudo.
I've Python 3.8 installed globally, but I'm trying to set Python 3.11 with venv. The steps to install Python 3.11 were, as indicated by 'Installing a custom version of Python 3 by Dreamhost':
cd /home/user_name/opt
wget https://www.python.org/ftp/python/3.11.0/Python-3.11.0.tgz
tar zxvf Python-3.11.0.tgz
cd Python-3.11.0
./configure --prefix=$HOME/opt/Python-3.11.0
make
make install
When running the make command I saw the following message:
The necessary bits to build these optional modules were not found:
_bz2 _curses _curses_panel
_lzma _tkinter _uuid
readline
To find the necessary bits, look in setup.py in detect_modules() for the module's name.
The following modules found by detect_modules() in setup.py have not
been built, they are *disabled* by configure:
_sqlite3
This is my detect_modules() in setup.py:
def detect_modules(self):
# remove dummy extension
self.extensions = []
# Some C extensions are built by entries in Modules/Setup.bootstrap.
# These are extensions are required to bootstrap the interpreter or
# build process.
self.detect_simple_extensions()
self.detect_test_extensions()
self.detect_readline_curses()
self.detect_crypt()
self.detect_openssl_hashlib()
self.detect_hash_builtins()
self.detect_dbm_gdbm()
self.detect_sqlite()
self.detect_platform_specific_exts()
self.detect_nis()
self.detect_compress_exts()
self.detect_expat_elementtree()
self.detect_multibytecodecs()
self.detect_decimal()
self.detect_ctypes()
self.detect_multiprocessing()
self.detect_tkinter()
self.detect_uuid()
# Uncomment the next line if you want to play with xxmodule.c
# self.add(Extension('xx', ['xxmodule.c']))
self.addext(Extension('xxlimited', ['xxlimited.c']))
self.addext(Extension('xxlimited_35', ['xxlimited_35.c']))
So after running the commands I just mentioned, I proceeded to setup the virtualenv to test Python 3.11:
cd /home/user_name/opt
/home/user_name/opt/Python-3.11.0/bin/python3 -m venv venv
source venv/bin/activate
And then I got the ModuleNotFoundError I mentioned at the start of the question.
Things I tried/saw:
When I checked my Python 3.11 directory, I saw that in /home/user_name/opt/Python-3.11.0/lib/python3.11/lib-dynload the cpython files for the modules I don't have are just not there, e.g: /home/user_name/opt/Python-3.11.0/lib/python3.11/lib-dynload/_sqlite3.cpython-311-x86_64-linux-gnu.so* where as in my Python 3.8, they're indeed there: /usr/lib/python3.8/lib-dynload/_sqlite3.cpython-38-x86_64-linux-gnu.so
I tried copying the cpython file from my 3.8 to my 3.11 and renaming it:
cp /usr/lib/python3.8/lib-dynload/_sqlite3.cpython-38-x86_64-linux-gnu.so /home/user_name/opt/Python-3.11.0/lib/python3.11/lib-dynload
mv /home/user_name/opt/Python-3.11.0/lib/python3.11/lib-dynload/_sqlite3.cpython-38-x86_64-linux-gnu.so /home/user_name/opt/Python-3.11.0/lib/python3.11/lib-dynload/_sqlite3.cpython-311-x86_64-linux-gnu.so
Then when I ran Python 3.11 I got the following error:
Python 3.11.0 (main, Jan 12 2023, 04:33:33) [GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import _sqlite3 # this works just fine
>>> import sqlite3 # throws error
356306 segmentation fault (core dumped) python
Tried recompiling Python and running ./configure --prefix=$HOME/opt/Python-3.11.0 --enable-loadable-sqlite-extensions and didn't work.
Hope I was clear enough and thanks in advance.

Error when importing igraph: ImportError:cannot import name community_to_membership

I'm not sure if this has been answered already, but I couldn't find anything related to my error, so I figured I would post a question.
I'm running on Linux and I've downloaded the igraph package using pip install python-igraph and made sure it is the python-igraph-0.7.1 package provided by Tamas Nepusz.
When I try to do import igraph, I get the following errors:
~$ python
Python 2.7.12 (default, Dec 4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import igraph
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/lashen/.local/lib/python2.7/site-packages/igraph/__init__.py", line 34, in <module>
from igraph._igraph import *
File "/home/lashen/.local/lib/python2.7/site-packages/igraph/igraph/__init__.py", line 37, in <module>
from igraph.cut import *
File "/home/lashen/.local/lib/python2.7/site-packages/igraph/cut.py", line 5, in <module>
from igraph.clustering import VertexClustering
File "/home/lashen/.local/lib/python2.7/site-packages/igraph/igraph/clustering.py", line 32, in <module>
from igraph import community_to_membership
ImportError: cannot import name community_to_membership
I'm not sure how to fix this import error. Did I install igraph incorrectly?? Seems like I got the right package ...
~$ pip show python-igraph
Name: python-igraph
Version: 0.7.1.post6
Summary: High performance graph data structures and algorithms
Home-page: http://pypi.python.org/pypi/python-igraph
Author: Tamas Nepusz
Author-email: tamas#cs.rhul.ac.uk
License: GNU General Public License (GPL)
Location: /home/lashen/.local/lib/python2.7/site-packages
Requires:
I have tried importing individual submodules of igraph, but to no avail. All submodule imports (e.g. import igraph.cut, import igraph._igraph) all result in the same error.
Why is this import error appearing? Any help is appreciated, thanks!!
You need to install sudo apt-get install libigraph0-dev first and then you can install igraph via pip install python-igraph --user

Can't import pyttsx in Python 2.7 on Ubuntu Linux 16

I'm trying to use pyttsx on Ubuntu Linux (64 bit, PC) with Python 2.7. I've successfully used pip to install the package
$sudo -H pip install pyttsx
$pip list
...
pyttsx (1.1)
...
When I try to import it the import call fails
$ python
Python 2.7.12 (default, Nov 20 2017, 18:23:56)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyttsx
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named pyttsx
I'm not sure why I'm getting this. When I search for the question here on Stack Overflow I encounter one other instance about the same error message on a Raspberry Pi. But the solution mentioned there doesn't bring resolution on my PC so I don't think it is not related.
Any ideal what I am missing that is preventing the import from being successful?
Make sure your pip is tied to your python installation by checking both path's.
In Pip check:
`pip --version`
pip 9.0.1 from C:\Python27\lib\site-packages (python 2.7)
Later in python:
import sys
print sys.executable
C:\Python27\python.exe
run which pip, which python, make sure they are same suite
when run pip install, it should feedback the package is installed in which path
run this code in python
import sys
for i in sys.path:
print i
to check if your syspath including that path or not.

ImportError: cannot import name _NONE

For some reason I'm getting this error when trying to import gevent inside my docker container:
# python
Python 2.7.10 (default, Oct 14 2015, 16:09:02)
[GCC 5.2.1 20151010] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import gevent
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/gevent/__init__.py", line 51, in <module>
from gevent.hub import get_hub, iwait, wait
File "/usr/local/lib/python2.7/dist-packages/gevent/hub.py", line 31, in <module>
from gevent._util import _NONE
ImportError: cannot import name _NONE
>>>
Which is odd because _util.py exists, it's in the dist-packages/gevent directory. When I did a pip install on another system, it works fine.
Anyone have any ideas what might be going on?
Have you upgraded gevent package recently or maybe installed it over old version?
I ran pip install --ignore-installed gevent to update old version of this package in my virtualenv, and then this error starts to appear.
I fixed it with pip uninstall gevent (two times to completely remove it) followed by pip install gevent.
I think you import gevnet this module is not you see it.
you can print something in you _util.py module.
if nothing print, I guess "/usr/local/lib/python2.7/dist-packages/gevent/hub.py" import _util is not in /usr/local/lib/python2.7/dist-packages/gevnet, and you can try to print sys.path to find real import path and fix it.
In my Method:
uninstall gevent.
use a another system gevnet or virtualenv package,and copy to you real(I guess you have a env path like '/usr/local/lib64')
exec python -c 'import gevent' test is ok.

issues with six under django?

I'm trying to use a package called vcrpy to accelerate the execution of my django application test suite. I'm using django 1.7 on Mac, with Python 2.7.
I added the following couple of lines to one of my tests:
import vcr
with vcr.use_cassette('recording.yaml'):
The result is an import error:
import vcr
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/vcr/__init__.py", line 2, in <module>
from .config import VCR
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/vcr/config.py", line 6, in <module>
from .cassette import Cassette
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/vcr/cassette.py", line 12, in <module>
from .patch import CassettePatcherBuilder
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/vcr/patch.py", line 8, in <module>
from .stubs import VCRHTTPConnection, VCRHTTPSConnection
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/vcr/stubs/__init__.py", line 9, in <module>
from six.moves.http_client import (
ImportError: No module named http_client
The problematic code in the VCR package itself is:
import six
from six.moves.http_client import (
HTTPConnection,
HTTPSConnection,
HTTPMessage,
HTTPResponse,
)
The funny thing: this code seems to run fine when I'm just running it from a plain python console, but it results in the above ImportError under Django or under the django manage.py shell.
Any idea what might be wrong?
( some additional details about the location of the six module:
When I'm running plain python console, I get the following:
Python 2.7.8 (v2.7.8:ee879c0ffa11, Jun 29 2014, 21:07:35)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import six
>>> print six.__file__
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/six.pyc
Doing the same thing, with import django; django.setup(), from manage.py shell results in exactly the same directory and same six.pyc file.
)
Maybe a little bit too late for the original question, but I came here trough Google so for future reference, here's my solution:
Problem
The problem I found is that mac os comes with not only python but also some packages pre-installed. Six is one of those packages and therein lies the conflict. The pre-installed six takes priority over the pip installed six, but pip still gives information based on what it itself has installed (e.g. six 1.9.0 vs 1.4.1).
pre-installed (version 1.4.1):
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/
pip installed (whatever you have installed, for me it was 1.9.0):
/Library/Python/2.7/site-packages/
You can check if this is the case for you by running:
$ python
>>> import six
>>> print six.__file__
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six.py'
Fixing it
The solution is actually quite simple, just put
export PYTHONPATH="/Library/Python/2.7/site-packages:$PYTHONPATH"
in your ~/.bashrc (or whatever file your shell uses). If you've configured your pip to install somewhere else, put that in the pythonpath instead.
I had a similar problem on Fedora 21. The reason for this was 2 installed version of the six 1.2.0 and 1.9.0. I solved this problem by the uninstall six, and re-installation latest version:
pip uninstall six
pip install six
Sounds like a multiple-versions conflict. I solved a similar error by downgrading my version of six (1.9.0 caused the error, as did 1.8.0 and 1.7.0). 1.6.0 works without error.
The error I was getting:
from six.moves import http_client
ImportError: No module named moves

Categories