This question already has answers here:
Importing installed package from script with the same name raises "AttributeError: module has no attribute" or an ImportError or NameError
(2 answers)
Closed 4 years ago.
I'm working with a project and somewhere down the line my I have to uninstall and reinstall numpy to fix a problem, but then this happened
Traceback (most recent call last):
File "ori2.py", line 3, in <module>
import numpy as np
File "/home/tsoi/Documents/environments/testvenv/my_venv/lib/python3.6/site-packages/numpy/__init__.py", line 142, in <module>
from . import add_newdocs
File "/home/tsoi/Documents/environments/testvenv/my_venv/lib/python3.6/site-packages/numpy/add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
File "/home/tsoi/Documents/environments/testvenv/my_venv/lib/python3.6/site-packages/numpy/lib/__init__.py", line 8, in <module>
from .type_check import *
File "/home/tsoi/Documents/environments/testvenv/my_venv/lib/python3.6/site-packages/numpy/lib/type_check.py", line 11, in <module>
import numpy.core.numeric as _nx
File "/home/tsoi/Documents/environments/testvenv/my_venv/lib/python3.6/site-packages/numpy/core/__init__.py", line 38, in <module>
from . import numeric
File "/home/tsoi/Documents/environments/testvenv/my_venv/lib/python3.6/site-packages/numpy/core/numeric.py", line 43, in <module>
import pickle
File "/home/tsoi/Documents/environments/testvenv/Traffic-rec/pickle.py", line 1
from ~/Documents/environments/testvenv/my_venv/lib/python3.6/site-packages/numpy import numpy as np
^
SyntaxError: invalid syntax
I have also tried
import sys
sys.path.append('~/Documents/environments/testvenv/my_venv/lib/python3.6/site-packages/numpy')
when importing numpy but the same error occured, This hasn't happened before and it's been annoying for me. This is the part of my code that I think is relevant to this problem:
from __future__ import print_function
#from comet_ml import Experiment
import numpy as np
import tensorflow as tf
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
from keras.datasets import fashion_mnist
from keras.models import Model
from keras.layers import Input, Dense
from keras.models import load_model
import cv2
import os
import glob
from os import path
import random
UPDATE:
Yeah you right user2357112
numpy is importing your file '/home/tsoi/Documents/environments/testvenv/Traffic-rec/pickle.py' instead of the pickle module, delete the file or rename it, then the problem should be fixed.
Related
ImportError: Traceback (most recent call last):
File "/opt/conda/lib/python3.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 58, in
on3.7/imp.py", line 342, in load_dynamic
return _load(spec)
ImportError: libcublas.so.10.0: cannot open shared object file: No such file or directory in the following code
import os
import tensorflow as tf
sess = tf.compat.v1.Session()
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"]="1"
import tensorflow as tf
config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth=True
import numpy as np
import pandas as pd
import pickle
from matplotlib import pyplot as plt
from PIL import Image
from IPython.display import Audio
from matplotlib.pyplot import imshow
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder
from sklearn.utils.class_weight import compute_class_weight
from tensorflow.contrib.keras import layers
from tensorflow.contrib.keras import models
from tensorflow.contrib.keras import layers
from tensorflow.contrib.keras import optimizers
from tensorflow.contrib.keras import callbacks
from tensorflow.contrib.keras import regularizers
from tensorflow.contrib.keras import models
%matplotlib inline
I tried importing Lasagne in my code which is as follows:
from __future__ import print_function
import sys
import os
import time
import string
import random
import pickle
import numpy as np
import theano
import theano.tensor as T
import lasagne
But on running the file I keep getting the following error:
File "main_cifar_100_theano.py", line 12, in <module>
import lasagne
File "/usr/local/lib/python3.7/dist-packages/lasagne/__init__.py", line 19, in <module>
from . import layers
File "/usr/local/lib/python3.7/dist-packages/lasagne/layers/__init__.py", line 5, in <module>
from .noise import *
File "/usr/local/lib/python3.7/dist-packages/lasagne/layers/noise.py", line 6, in <module>
from theano.sandbox.rng_mrg import MRG_RandomStreams as RandomStreams
ImportError: cannot import name 'MRG_RandomStreams' from 'theano.sandbox.rng_mrg' (/usr/local/lib/python3.7/dist-packages/theano/sandbox/rng_mrg.py)
Please help me resolve this issue.
I have been trying to get a tutorial working, that uses the keras_squeezenet package in Python. When I try to run one of the files, I get an ImportError. I have tried both adding a line to import keras, and to import keras_squeezenet at the top, but this hasn't fixed the problem. The full error message:
File "train_model.py", line 4, in <module>
from keras_squeezenet import SqueezeNet
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\keras_squeezenet\__init__.py", line 2, in <module>
from keras_squeezenet.squeezenet import SqueezeNet
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\keras_squeezenet\squeezenet.py", line 1, in <module>
from keras_applications.imagenet_utils import _obtain_input_shape
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\keras_applications\imagenet_utils.py", line 14, in <module>
backend = get_keras_submodule('backend')
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\keras_applications\__init__.py", line 34, in get_keras_submodule
raise ImportError('You need to first `import keras` '
ImportError: You need to first `import keras` in order to use `keras_applications`. For instance, you can do:
import keras
from keras_applications import vgg16
Or, preferably, this equivalent formulation:
from keras import applications
If anyone could please help me with this error, it would be greatly appreciated.
You need first to write the following before the line from keras_squeezenet:
import keras
from keras_applications import vgg16
from __future__ import absolute_import, division, print_function, unicode_literals
import numpy as np
import tensorflow as tf
import tensorflow_hub as hub
import tensorflow_datasets as tfds
I'm following tensorflow but when I try to import tensorflow_hub, error occurs like
Traceback (most recent call last):
File "C:/PythonProjects/191217_TensorFlow_Project/model3.py", line 7, in <module>
import tensorflow_hub as hub
File "C:\Users\UserK\Anaconda3\envs\191217_TensorFlow_Project_1\lib\site-packages\tensorflow_hub\__init__.py", line 53, in <module>
from tensorflow_hub.keras_layer import KerasLayer
File "C:\Users\UserK\Anaconda3\envs\191217_TensorFlow_Project_1\lib\site-packages\tensorflow_hub\keras_layer.py", line 39, in <module>
class KerasLayer(tf.keras.layers.Layer):
AttributeError: 'function' object has no attribute 'layers'
I don't have any file named 'tensorflow_hub.py', so i don't know why it happens.
Sorry if this is a dumb question but I'm trying to import and open a CSV using pandas in Python. Whenever I hit run I get the syntax error "cannot import name 'unicode_literals'". I have no idea why that is happening and I haven't been able to find any source online which details what this error means.
This is my code:
import pandas as pd
with open(r"FILEPATH\File.csv") as rawData:
pd.read_csv(rawData)
Here is the Error:
C:\Anaconda3\python.exe "FILEPATH"
Traceback (most recent call last):
File "FILEPATH/Main.py", line 1, in <module>
import pandas as pd
File "C:\Anaconda3\lib\site-packages\pandas\__init__.py", line 7, in <module>
from . import hashtable, tslib, lib
File "pandas\src\numpy.pxd", line 157, in init pandas.hashtable (pandas\hashtable.c:22997)
File "C:\Anaconda3\lib\site-packages\numpy\__init__.py", line 107, in <module>
from __future__ import division, absolute_import, print_function
File "C:\Anaconda3\lib\__future__.py", line 23, in <module>
from __future__ import unicode_literals
ImportError: cannot import name 'unicode_literals'
cannot import name 'unicode_literals'
Any suggestions for why this isn't working would be greatly appreciated.
You're on the right track! The only thing you have to do is add another parameter to open(). This would yield:
import pandas as pd
with open(r"FILEPATH\File.csv", encoding='utf-8') as rawData:
pd.read_csv(rawData)