Issues
id
Do not use the variable name id
. It is the name of a python built
in function which returns the address of an object.
For details see ‘id’ is a bad variable name in Python
module object has no attribute
I have bumped into this error when I name a file the same as some external class
I am trying to import e.g. script file name csv.py
and import csv
at
the top of the script.
'module' object has no attribute
Note: Make sure you delete the compiled .pyc
file as well!
UnboundLocalError
Python Functions: Assignments And Scope
Quite a nasty one this… here is the code which threw the exception:
cache_clear = False
class TestRunner:
def __call__(self):
if cache_clear:
# do something...
cache_clear = True
The issue here is to do with the python interpreter deciding if
cache_clear
is a local or global inside the __call__
method.
To solve the problem, we tell the method that cache_clear
is a global
variable by using the global
statement to list the (global) variables:
cache_clear = False
class TestRunner:
def __call__(self):
global cache_clear
if cache_clear:
# do something...
cache_clear = True
SyntaxError: Non-ASCII character in file, but no encoding declared
The source code contained the pound character:
SyntaxError: Non-ASCII character in file 'gateway.py', but no encoding declared;
see http://www.python.org/peps/pep-0263.html for details
To solve the issue, I put the following at the top of the source file:
# -*- coding: utf-8 -*-
ValueError: bad marshal data (unknown type code)
When running py.test
…
This is something to do with .pyc
files. I tried removing them:
find . -name '*.pyc' -delete
… but this did not solve the problem.
I had a module included from a requirements file:
-e ../base
I removed the .pyc
files from this app, and all was OK once more.
Might be worth removing __pycache__
folders as well if removing the
pyc
doesn’t help.