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
Related
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.
I'm trying to get PySide6 to run in Python 3.8.0. Installation went fine:
C:\>pip install pyside6
Collecting pyside6
Using cached PySide6-6.0.0-6.0.0-cp36.cp37.cp38.cp39-none-win_amd64.whl (62.4 MB)
Collecting shiboken6==6.0.0
Using cached shiboken6-6.0.0-6.0.0-cp36.cp37.cp38.cp39-none-win_amd64.whl (2.3 MB)
Installing collected packages: shiboken6, pyside6
Successfully installed pyside6-6.0.0 shiboken6-6.0.0
but i'm hit with an import error:
Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:37:50) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import PySide6
PySide6/__init__.py: Unable to import shiboken6 from , C:\Python38\python38.zip, C:\Python38\DLLs, C:\Python38\lib, C:\Python38, C:\Python38\lib\site-packages
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python38\lib\site-packages\PySide6\__init__.py", line 107, in <module>
_setupQtDirectories()
File "C:\Python38\lib\site-packages\PySide6\__init__.py", line 57, in _setupQtDirectories
import shiboken6
File "C:\Python38\lib\site-packages\shiboken6\__init__.py", line 25, in <module>
from .shiboken6 import *
ImportError: DLL load failed while importing shiboken6: The specified procedure could not be found.
What did i do wrong?
What did i do wrong?
I don't believe you did anything wrong.
I think this was a known bug in shiboken6 versions <=6.0.0. It was reported partly in the PySide bug tracker: PYSIDE-932.
The issue appeared to be that they were attempting to import from a zip file. From the comments in version 6.3.0 of shiboken6.__init__:
# PYSIDE-932: Python 2 cannot import 'zipfile' for embedding while being imported, itself.
# We simply pre-load all imports for the signature extension.
# Also, PyInstaller seems not always to be reliable in finding modules.
# We explicitly import everything that is needed:
The comment says Python 2 cannot import, but it should read PySide2 cannot import because the issue came from Python 3.6 using PySide 2.
The Qt Docs mention using a newer version of virtualenv to fix this problem, but I do not believe virtualenv is the culprit. Moreover, the latest Python 3 versions ship with and use venv. Instead, I believe this was an issue with importing zip files. It may have been resolved when Barry Warsaw/Brett Cannon and their team introduced importlib.resources in newer versions of Python 3, which can particularly help with zip files.
See their talk here:
Barry Warsaw - Get your resources faster, with importlib.resources - PyCon 2018
Ultimately, I believe this was resolved in the latest version of shiboken6 (i.e. 6.3.0).
Give that a try and see if that works.
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.
I've been having difficulty with Python recently, mainly since I think I had several versions and conflicts (due to Anaconda installs, canopy installs etc.). So I cleaned those out.
I reinstalled python (2.7) via brew.
I reinstalled numpy and matplotlib via pip.
I also reinstalled astropy and h5py via pip.
However, I get a clean import of numpy and matplotlib, but not of astropy and h5py:
~ > python
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> import matplotlib
>>> import astropy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named astropy
>>> import h5py
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named h5py
>>>
My suspicion is that your pip executable is not linked to your python executable, which means when you run pip install astropy it is installing it in the site-packages for a different python.
One way to make sure you're using the correct pip is to not use
$ pip install astropy
but instead use
$ python -m pip install astropy
If this fails, it probably means that you don't have pip installed for the python instance you're using, and you need to install it (note that for Python 2 version 2.7.9 or later, or Python 3 version 3.4 or later, pip comes bundled with Python).
If this still doesn't work, then something stranger is going on. It may be due to having $PYTHONPATH or $LD_LIBRARY_PATH/$DYLD_LIBRARY_PATH set in a way that interferes with your python imports. In this case, you could clear these variables and try again. Otherwise, I'd consider using a package bundle such as conda or canopy. It makes these kinds of installation issues much smoother.
Edit: I see now that you've used conda and canopy, and you suspect these were causing your problems. Conda and canopy, by design, both sandbox their python installations so that they shouldn't get interference from other installs in your system, unless you force such interference by setting the environment variables I mentioned above. I'd suggest reinstalling conda and wiping those environment variables from your bash/csh startup script.
Do the following on the default Python install on Mac OS X 10.5 (Leopard) w/ Developer Tools:
noel ~ : python
Python 2.5.1 (r251:54863, Jan 13 2009, 10:26:13)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import bsddb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/bsddb/__init__.py", line 51, in <module>
import _bsddb
ImportError: No module named _bsddb
nice, huh? How do I fix this without giving up and installing/configuring/maintaining my own Python package as per TMNC's suggestion or using MacPorts etc?
Edit
I've gone around the problem by installing Python2.4 and BSDDB via MacPorts.
My question still stands: why is the default install broken and is it possible to fix it.
Follow the instructions at http://marc-abramowitz.com/archives/2007/11/28/hacking-os-xs-python-dbhash-and-bsddb-modules-to-work/ .
The patch did not work for me and I had to replace the bsddb folder in
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
with the bsddb3 in:
/usr/local/lib/python2.7/site-packages/bsddb3
Make sure you backup the bsddb folder just in case.
This pain persists on OSX 10.8. I could not install bsddb3 using macports py-bsddb3 into a virtualenv. What was very simple and did work is:
install db53 from macports
download and unpack bsddb3 source (https://pypi.python.org/pypi/bsddb3/6.1.0)
sudo python setup.py –berkeley-db-incdir=/opt/local/include/db53 –berkeley-db-libdir=/opt/local/lib/db53 install
A solution I found was to install ActivePython, dig into its library (located in /Library/Frameworks/Python.framework/Versions/....your version here..../lib) and copy and paste the _bsddb.so file into my OS X 10.6 native python 2.6 install directory (/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/).
That fixed the missing _bsddb issue leaving me with a working version of bsddb in my native python install. Then, I just uninstalled ActivePython (instructions here)
The error I had was "No module named _bsddb". It turn out I didn't need to upgrade my bsddb. I wasusing the .pkl file created in windows. After renaming the pkl file to get it out the way Mac OSX Python recreated a new .pkl.db file and now it works perfectly.