It seems that if a file is called io.py and it imports scipy.ndimage, the latter somehow ends up failing to find its own submodule, also called io:
$ echo "import scipy.ndimage" > io.py
$ python io.py
Traceback (most recent call last):
File "io.py", line 1, in <module>
import scipy.ndimage
File "/usr/lib/python2.7/dist-packages/scipy/__init__.py", line 70, in <module>
from numpy import show_config as show_numpy_config
File "/usr/lib/python2.7/dist-packages/numpy/__init__.py", line 153, in <module>
from . import add_newdocs
File "/usr/lib/python2.7/dist-packages/numpy/add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
File "/usr/lib/python2.7/dist-packages/numpy/lib/__init__.py", line 22, in <module>
from .npyio import *
File "/usr/lib/python2.7/dist-packages/numpy/lib/npyio.py", line 4, in <module>
from . import format
File "/usr/lib/python2.7/dist-packages/numpy/lib/format.py", line 141, in <module>
import io
File "/tmp/rm_me/io.py", line 1, in <module>
import scipy.ndimage
File "/usr/lib/python2.7/dist-packages/scipy/ndimage/__init__.py", line 172, in <module>
from .filters import *
File "/usr/lib/python2.7/dist-packages/scipy/ndimage/filters.py", line 37, in <module>
from scipy.misc import doccer
File "/usr/lib/python2.7/dist-packages/scipy/misc/__init__.py", line 45, in <module>
from .common import *
File "/usr/lib/python2.7/dist-packages/scipy/misc/common.py", line 10, in <module>
from numpy import exp, log, asarray, arange, newaxis, hstack, product, array, \
ImportError: cannot import name exp
Is this a bug in SciPy, or am I using Python wrong?
Update: I think less surprising behavior would be if import mod2 in mod1 resolved paths relative to mod1 rather than relative to whomever imported mod1.
The simple fix is to avoid naming your module io, because it's conflicting with a core library module name.
It's not really a bug in numpy, but user error: just as we shouldn't use list as a variable name because it's shadowing the builtin list name, we shouldn't use io as a module name because it's shadowing the core library io module name.
See this part:
File "/usr/lib/python2.7/dist-packages/numpy/lib/format.py", line 141, in <module>
import io
Here numpy tries to import the io module. This is intended to find the stdlib io because of the absolute import - a numpy submodule would have been loaded with relative import like you can see in the traceback where there is another line beginning from .npyio import *.
Of course your own io.py module is found first, because in the current working directory is generally the first entry in sys.path. Whoops!
You're using python wrong.
Before you create any top-level python module or package, you should make sure there isn't already a module or package by that name.
The best solution here is to not use top-level modules, and instead put everything in a single top-level package (i.e. directory with an __init__.py file) named after your project.
To check if a top-level module or package exists, you could try importing it in the interpreter, or running pydoc name from the shell.
It is worth noting that there is a very similar error that can happen that is not your fault, if a package uses absolute-style import syntax to perform a relative import. This "feature" is removed in Python3.
Related
I am using win 64 with python 3.9 version. I have also installed numpy version 1.19.3 on my system but I'm still getting the error below.
How do I resolve this error?
from . import _distributor_init
File "C:\Users\Uttam\AppData\Local\Programs\Python\Python39\lib\site-packages\numpy\_distributor_init.py", line 9, in <module>
import glob
File "C:\Users\Uttam\AppData\Local\Programs\Python\Python39\lib\glob.py", line 4, in <module>
import re
File "C:\Users\Uttam\AppData\Local\Programs\Python\Python39\lib\re.py", line 127, in <module>
import functools
File "C:\Users\Uttam\AppData\Local\Programs\Python\Python39\lib\functools.py", line 18, in <module>
from collections import namedtuple
File "C:\Users\Uttam\AppData\Local\Programs\Python\Python39\lib\collections\__init__.py", line 37, in <module>
from operator import eq as _eq
ImportError: cannot import name 'eq' from 'operator' (D:\Python Pro\operator.py)
Change your operator.py name in Python Pro folder to another name.
Because numpy also have operator.py, so it makes numpy ambiguous to get the operator.py
import numpy as np
File "/home/anirrudh/.virtualenvs/ml4t2/local/lib/python2.7/site-packages/numpy/__init__.py", line 180, in <module>
from . import add_newdocs
File "/home/anirrudh/.virtualenvs/ml4t2/local/lib/python2.7/site-packages/numpy/add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
File "/home/anirrudh/.virtualenvs/ml4t2/local/lib/python2.7/site-packages/numpy/lib/__init__.py", line 8, in <module>
from .type_check import *
File "/home/anirrudh/.virtualenvs/ml4t2/local/lib/python2.7/site-packages/numpy/lib/type_check.py", line 11, in <module>
import numpy.core.numeric as _nx
File "/home/anirrudh/.virtualenvs/ml4t2/local/lib/python2.7/site-packages/numpy/core/__init__.py", line 57, in <module>
from numpy.testing import Tester
File "/home/anirrudh/.virtualenvs/ml4t2/local/lib/python2.7/site-packages/numpy/testing/__init__.py", line 14, in <module>
from .utils import *
File "/home/anirrudh/.virtualenvs/ml4t2/local/lib/python2.7/site-packages/numpy/testing/utils.py", line 15, in <module>
from tempfile import mkdtemp
File "/usr/lib/python2.7/tempfile.py", line 35, in <module>
from random import Random as _Random
File "/home/anirrudh/OMSCS/Courses/ML4T/ml4t/mc3_p2/random.py", line 5, in <module>
import matplotlib.pyplot as plt
File "/home/anirrudh/.virtualenvs/ml4t2/local/lib/python2.7/site-packages/matplotlib/__init__.py", line 122, in <module>
from matplotlib.cbook import is_string_like, mplDeprecation, dedent, get_label
File "/home/anirrudh/.virtualenvs/ml4t2/local/lib/python2.7/site-packages/matplotlib/cbook.py", line 33, in <module>
import numpy.ma as ma
File "/home/anirrudh/.virtualenvs/ml4t2/local/lib/python2.7/site-packages/numpy/ma/__init__.py", line 44, in <module>
from . import core
File "/home/anirrudh/.virtualenvs/ml4t2/local/lib/python2.7/site-packages/numpy/ma/core.py", line 30, in <module>
import numpy.core.umath as umath
AttributeError: 'module' object has no attribute 'core'
I am working on Ubuntu 14.04. I tried reinstalling numpy, created a new virtualenv, but it isn't helping. I get the same error outside the virtualenv as well. It stopped working suddenly. It was fine until a little while ago.
(Answer posted on behalf of the OP).
Update: Resolved.
I had created a python file for one of my projects name random.py and when numpy is imported, it was calling this file instead of it's own library file hence resulting in the error. I had to delete/rename random.py which I created and that fixed the issue.
Try running it in Python 2 instead of Python 3 - you may have the older version of Spark
This happened to me in Windows with Anaconda. I fixed it by removing the python3 path from the environment variable PATH.
Another common problem here (which I had)...check to make sure you dont have a PYTHONPATH env var pointing to a different version of python site-packages (this happened because I updated my python version and didn't change this...it loads from PYTHONPATH first).
It happened to me in a specific ipynb, once I crated a copy of the notebook with a different name I was able to import numpy.
I have installed python in my mac. When I type python3in terminal and then import requests and bs4 it imports it and run the program correctly.
But when I run it on python file as python3 file_name.py, it gives the following error:
import requests
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/__init__.py", line 52, in <module>
from .packages.urllib3.contrib import pyopenssl
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/packages/__init__.py", line 27, in <module>
from . import urllib3
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/packages/urllib3/__init__.py", line 8, in <module>
from .connectionpool import (
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/packages/urllib3/connectionpool.py", line 3, in <module>
import logging
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/__init__.py", line 28, in <module>
from string import Template
File "/Users/dark_archer/Desktop/src/string.py", line 1, in <module>
n1,n2=map(int,input().split())
ValueError: not enough values to unpack (expected 2, got 0)
I got the same error with both python 3.5 and python 3.6.
The issue is that you named a module string.py so it's confusing the importer because the logging module is also trying to import something from the standard library module string.py. This causes an issue known as "name shadowing" where your locally defined module is loaded instead of the the standard library module.
When your version of string.py gets imported it triggers the code which is causing your error.
As an easy fix, try to rename your string.py module to something else.
For more info on name shadowing check out the "The name shadowing trap" section of this link: http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html
import numpy as np
File "/home/anirrudh/.virtualenvs/ml4t2/local/lib/python2.7/site-packages/numpy/__init__.py", line 180, in <module>
from . import add_newdocs
File "/home/anirrudh/.virtualenvs/ml4t2/local/lib/python2.7/site-packages/numpy/add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
File "/home/anirrudh/.virtualenvs/ml4t2/local/lib/python2.7/site-packages/numpy/lib/__init__.py", line 8, in <module>
from .type_check import *
File "/home/anirrudh/.virtualenvs/ml4t2/local/lib/python2.7/site-packages/numpy/lib/type_check.py", line 11, in <module>
import numpy.core.numeric as _nx
File "/home/anirrudh/.virtualenvs/ml4t2/local/lib/python2.7/site-packages/numpy/core/__init__.py", line 57, in <module>
from numpy.testing import Tester
File "/home/anirrudh/.virtualenvs/ml4t2/local/lib/python2.7/site-packages/numpy/testing/__init__.py", line 14, in <module>
from .utils import *
File "/home/anirrudh/.virtualenvs/ml4t2/local/lib/python2.7/site-packages/numpy/testing/utils.py", line 15, in <module>
from tempfile import mkdtemp
File "/usr/lib/python2.7/tempfile.py", line 35, in <module>
from random import Random as _Random
File "/home/anirrudh/OMSCS/Courses/ML4T/ml4t/mc3_p2/random.py", line 5, in <module>
import matplotlib.pyplot as plt
File "/home/anirrudh/.virtualenvs/ml4t2/local/lib/python2.7/site-packages/matplotlib/__init__.py", line 122, in <module>
from matplotlib.cbook import is_string_like, mplDeprecation, dedent, get_label
File "/home/anirrudh/.virtualenvs/ml4t2/local/lib/python2.7/site-packages/matplotlib/cbook.py", line 33, in <module>
import numpy.ma as ma
File "/home/anirrudh/.virtualenvs/ml4t2/local/lib/python2.7/site-packages/numpy/ma/__init__.py", line 44, in <module>
from . import core
File "/home/anirrudh/.virtualenvs/ml4t2/local/lib/python2.7/site-packages/numpy/ma/core.py", line 30, in <module>
import numpy.core.umath as umath
AttributeError: 'module' object has no attribute 'core'
I am working on Ubuntu 14.04. I tried reinstalling numpy, created a new virtualenv, but it isn't helping. I get the same error outside the virtualenv as well. It stopped working suddenly. It was fine until a little while ago.
(Answer posted on behalf of the OP).
Update: Resolved.
I had created a python file for one of my projects name random.py and when numpy is imported, it was calling this file instead of it's own library file hence resulting in the error. I had to delete/rename random.py which I created and that fixed the issue.
Try running it in Python 2 instead of Python 3 - you may have the older version of Spark
This happened to me in Windows with Anaconda. I fixed it by removing the python3 path from the environment variable PATH.
Another common problem here (which I had)...check to make sure you dont have a PYTHONPATH env var pointing to a different version of python site-packages (this happened because I updated my python version and didn't change this...it loads from PYTHONPATH first).
It happened to me in a specific ipynb, once I crated a copy of the notebook with a different name I was able to import numpy.
I have a very wierd problem.
When i run the file from a different directory it runs fine.
but other wise i get this error
from textprocessor import *
File "/home/mohit/Documents/analysis/categorization/textprocessor.py", line 2, in <module>
import nltk
File "/usr/local/lib/python2.7/dist-packages/nltk/__init__.py", line 101, in <module>
import ccg
File "/usr/local/lib/python2.7/dist-packages/nltk/ccg/__init__.py", line 15, in <module>
from chart import *
File "/usr/local/lib/python2.7/dist-packages/nltk/ccg/chart.py", line 33, in <module>
from nltk.parse.api import *
File "/usr/local/lib/python2.7/dist-packages/nltk/parse/__init__.py", line 46, in <module>
from featurechart import *
File "/usr/local/lib/python2.7/dist-packages/nltk/parse/featurechart.py", line 24, in <module>
import nltk.data
File "/usr/local/lib/python2.7/dist-packages/nltk/data.py", line 41, in <module>
import zipfile
File "/usr/lib/python2.7/zipfile.py", line 460, in <module>
class ZipExtFile(io.BufferedIOBase):
AttributeError: 'module' object has no attribute 'BufferedIOBase'
I am tryin to import nltk module
You've called a script in the same directory "io.py", and this is interfering with the io module in the stdlib. Rename it.
You must have a script in your folder named nltk.py, io.py, zipfile.py, or some such. When you run the file in the same directory, it is imported instead of the appropriate python or nltk module.
Where is nltk? Is it pure Python code? You may want to put that in site-wide modules folder or in your cwd.