You are currently viewing How to resolve errors in Python
How to resolve errors in Python

How to resolve errors in Python

Loading

Special thanks to stackoverflow which helped in solving these errors. Errors are important part of programming. Solving such errors helps one to become good developer. Not only it makes you capable to accept new challenge and improve your clarity towards programming. I faced some of the problems daily and are listed below. Hope this will help you. You can share your ideas in the comment section. Some of the Type Error as well Attribute errors are resolved.

Error1:

ImportError: module ‘setuptools.dist’ has no ‘check_specifier’ attribute

Solution:

One of the reason is older setuptools. Please upgrade the setuptools using the below command

 sudo pip install setuptools --upgrade
 sudo pip3 install setuptools --upgrade

Note: please see which version of pip you are using and apply the command accordingly.

Error2:

pip3: command not found but python3-pip is already installed

Solution:

pip3 is installed but error is saying that command is not found. One thing that you can do is to locate pip3. Problem may is that pip3 folder is properly generated in /usr/bin so one of the solution will be uninstall it and reinstall it using the following commands

Uninstalling pip3 python

sudo apt-get remove python3-pip

Installing pip3 python

sudo apt-get install python3-pip

Error 3:

AttributeError: ‘_Environ’ object has no attribute ‘has_key’

Solution:

This error comes while you are working with different versions of python. In Python 2 dictionaries has has_key method while it does not exits in python 3. So please see your python version first.
If you have two versions of python installed you may run the file with python 2 instead of python 3. Solving such error while referring to one problem solved in the stack overflow

Statement:

if os.environ.has_key(‘SERVER_SOFTWARE’)

If you run it using python 3, it will hit the error because has_key is not defined in python 3 dictionary so please change the above to

if ‘SERVER_SOFTWARE’ in os.environ

Source of example: Stackoverflow

Suggestion: When anaconda python and default python is both available, opening python in terminal picks the anaconda python instead of default python

Solution:

This is because while installing anaconda to the system it adds its python path to the file .bashrc. SO remove the path. You could see the path while vising line with something like this export PATH=$HOME/anaconda/bin:$PATH and add alias python=/usr/bin/python. May be this will work for you

Error 5:

Error:TypeError: the JSON object must be str, not ‘bytes’

Reference: I got such error in the following line

jsonobj = json.loads(self.request.body) Byte is a raw binary data while JSON read string data

Approach:

I replaced json.loads(self.request.body) with json.loads(self.request.body.decode(”utf-8”))

Some note on UTF-8:

UTF-8 is a method for encoding Unicode characters using 8-bit sequences. Unicode was created as a standard to represent characters from nearly all writing systems. UTF-8 or ASCII characters are used to convert the characters into binary so that computer or machine can understand the data.

Error 6:

Only scalar arrays can be converted to a scalar index.

Describing the error

To describe this error, please have a look at the below code

import numpy as np
array_to_use=np.arange(1,4,dtype=np.int)
array_np =np.arange(12).reshape(3,4)
#Now you want to slice array using array_to_use
Output = array_np[:,0:array_to_use]
print(Output)

Output of the code

TypeError: only integer scalar arrays can be converted to a scalar index

Why this error comes???

This error came as you cannot use the numpy array (array_to_use) to do slicing/vectorization. We need to supply some integer there so that end limit can be described.

How to Correct it??

Please have a look at the solution as below. We need to iterate the array as number is needed for slicing not numpy array.

print ([array_np [:,:j] for j in array_to_use])

Output of the code

[array([[0],
[4],
[8]]), array([[0, 1],
[4, 5],
[8, 9]]), array([[ 0, 1, 2],
[ 4, 5, 6],
[ 8, 9, 10]])]

Error 7:

multiple repeat at position

Why this error come: To elaborate with the error let us take an example

string= "ai sangam, sangammm, machine learning company"
output = re.findall('ga{1}+',string)

Error: Please see the error from here

https://drive.google.com/file/d/1Fk66dKD7AkLJWj4LaVjS5bPklKqlbnUP/view?usp=sharing as error is large

What is the reason of this error: You have used both “{}” and the “+” together which created the error. Please use one

Solution 1: Please see the new code for it using only {}

output = re.findall(‘ga{1}’,string)

Solution 2: Please use only +.

output = re.findall(‘ga+’,string)

Solution 3: You can place () between + and {} to resolve the error. Please see the below code.

output = re.findall(‘(ga{1})+’,string)

Error 8: No Module named ‘Object Detection’ (Tensorflow Object detection api)

When such errors are encountered: Such error is encountered when you are making your custom object detection model using tensorflow api. You can download the model by visiting below command.

https://github.com/tensorflow/models

For better visibility please have a look at the screenshot from here.

I myself have encountered such error while creating model from export_inference_graph.py.

Solution: Since this is the problem related to object_detection library, we need to install it by running the below command inside the directory models/research

sudo python3 setup.py install

Error 9: No ‘Access-Control-Allow-Origin’ header is present

Solution: To deal with such error, please enable CORS. Read below to get more understanding about CORS.
What is CORS: Cross-Origin Resource Sharing is a mechanism that uses additional http headers to let the browser allow web application running at different origin to access server resources at different origin. From origin I mean domain, protocol and port number.

HTTP RESPONSE HEADERS
“Access-Control-Allow-Origin”, “*”: It is a wildcard which tells the browser to allow any origin to access the resource.
‘Access-Control-Allow-Methods’,’name of method’: It specifies the method allowed when accessing resources.

Preflighed request
This implies that request is checked whether it is safe or not before sending it. Request is preflighed if it uses following methods such as PUT, DELETE, CONNECT, OPTIONS, TRACE and PATCH.

Add this in code.

def set_default_headers(self):
        print "setting headers!!!"
        self.set_header("Access-Control-Allow-Origin", "*")
        self.set_header("Access-Control-Allow-Headers", "x-requested-with")
        self.set_header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS')

You may also look at the proper solution for this by relating your error with this error at stackoverflow.

Error 10: 0% [Waiting for headers]

Please have a look at the screenshot attached here to look at how the error looks like from here

Try with this command to solve the error.

sudo apt-get clean

On executing such command, some times you encounter another error which is as below.
Could not get lock /var/lib/apt/lists/lock – open (11: Resource temporarily unavailable). Please see the screenshot for such from here. To resolve such error, please find all the apt processes using the below command and kill that.

Command to find apt process

ps -A | grep apt

Command to kill apt process

sudo kill -9 ProcessId(PID)

After this please run the below command again

sudo apt-get clean

If everything goes fine, you will not face the error. This will also help to remove error of fetching the archives.

Error 11: http://www.getdeb.net/ubuntu wily-getdeb InRelease  403 Forbidden [IP: 143.95.32.90 80]

When Such Error arrive: Generally while you are updating your apt i.e sudo apt-get update such errors come. It is very simple to remove these. Please open software & Updates. You will see the window similar to one which is shown in the screenshot link. Now please choose the tab Other Software from there and look for any entry similar to wily-getdeb. If you face any problem, please see the link for this from here. Untick it and close it. I hope problem will be resolved.

Error 12: Import Error: No module named pywrap_tensorflow

Solution1:

First of all please check the tensorflow version by executing the below code

import tensorflow as tf
print(tf.__version__)

You can try with the below command

pip install tensorflow --upgrade --force-reinstall

Solution 2:

There is another solution which can work

pip uninstall tensorflow

Now please install tensorflow==1.5.0

Solution 3:

Some times please see the version of python and tensorflow you are using. To check which version of python you are using please type python -V. You have to check whether your tensorflow version is compatible with the version of python you are using.

Error 13: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: ‘/usr/local/lib/python3.5/dist-packages/decorator.py’ Consider using the `–user` option or check the permissions.

Solution: This error clearly reflects that you are trying to install the package in the system folder where you have not access to install it. Location for the system folder is /usr/local/lib/python3.5/dist-packages/. You have these different option to install the package
Option 1: This is the most recommended option.You can use the virtual Environment to work with the project.

To install virtual environment, please type the below command

pip3 install virtualenv

To use the virtual environment, please type the below command

python3 -m virtualenv **name of virtual environment** 

You can activate the virtual environment by going inside the virtual environment directory and typing

cd bin

Now you can see the activate script there. Please type

source activate

Option2: You can install the package in the user folder Location for this path is /home/ai/.local/lib/python2.7/site-packages. For this please add the user flag with the pip command

sudo pip3 install **name of package** 

Option 3: If you want to install in the system folder, you can do it with the sudo command even though it is not recommended. You have two good solution above so you can go for these but if you want this option, please type sudo with pip as below

sudo pip3 install **name of package** 

Error 14: ImportError: No module named docutils.core

Solution: Docutils is a open source text processing system for processing plaintext documenation into useful formats such as HTML, Latex or XML. I would like you to visit the site for this as http://docutils.sourceforge.net/ where you can know the things in details.

To install this, pip can be used. It is important to know about pip so that you can know how to install it using pip. Pip is a package manager for python modules. Packages which are found in Python Package Index are installed using pip. One can also say that it is a replacement to easy_install. If you have not installed pip, first please install pip using the below command

sudo apt-get install python-pip

If you are using python 3, please execute the below command

sudo apt-get install python3-pip

Now we can install the above module using pip as

pip3 install docutils

Leave a Reply