I wrote this funcion on a utils.py located on the app direcroty:
我写了这篇关于utils的文章。py位于app direcroty:
from bm.bmApp.models import Client
def get_client(user):
try:
client = Client.objects.get(username=user.username)
except Client.DoesNotExist:
print "User Does not Exist"
return None
else:
return client
def to_safe_uppercase(string):
if string is None:
return ''
return string.upper()
Then when i use the function to_safe_uppercase on my models.py file, by importing it in this way:
然后当我在模型上使用to_safe_uppercase函数时。py文件,通过这种方式导入:
from bm.bmApp.utils import to_safe_uppercase
I got the python error:
我得到了python错误:
from bm.bmApp.utils import to_safe_uppercase
ImportError: cannot import name to_safe_uppercase
I got the solution for this problem when i change the import statement for:
当我将导入语句改为:
from bm.bmApp.utils import *
But i can't understand why is this, why when i import the specific function i got the error?
但是我不明白为什么,为什么当我导入特定的函数时我得到了错误?
3 个解决方案
#1
10
You are doing what is known as a Circular import.
您正在执行循环导入。
models.py:
models.py:
from bm.bmApp.utils import to_safe_uppercase
utils.py:
utils.py:
from bm.bmApp.models import Client
Now when you do import bm.bmApp.models
The interpreter does the following:
现在当你导入bmapp的时候。模型译员做以下工作:
-
models.py - Line 1
: try to importbm.bmApp.utils
- 模型。py -第1行:尝试导入bm.bmApp.utils
-
utils.py - Line 1
: try to importbm.bmApp.models
- 跑龙套。py - Line 1:尝试导入bm.bmApp.models
-
models.py - Line 1
: try to importbm.bmApp.utils
- 模型。py -第1行:尝试导入bm.bmApp.utils
-
utils.py - Line 1
: try to importbm.bmApp.models
- 跑龙套。py - Line 1:尝试导入bm.bmApp.models
- ...
- …
The easiest solution is to move the import inside the function:
最简单的解决方案是将导入移动到函数内部:
utils.py:
utils.py:
def get_client(user):
from bm.bmApp.models import Client
try:
client = Client.objects.get(username=user.username)
except Client.DoesNotExist:
print "User Does not Exist"
return None
else:
return client
def to_safe_uppercase(string):
if string is None:
return ''
return string.upper()
#2
7
You are creating a circular import.
您正在创建循环导入。
utils.py
from bm.bmApp.models import Client
# Rest of the file...
models.py
from bm.bmApp.utils import to_safe_uppercase
# Rest of the file...
I would suggest your refactor your code so that you don't have a circular dependency (i.e. utils should not need to import models.py or vice versa).
我建议您重构代码,这样就不会有循环依赖(例如,utils不需要导入模型)。py,反之亦然)。
#3
0
I'm not sure I can explain the Import error, but I have three ideas. First, your function needs tweaking. You've used a reserved word 'string' as an argument. Consider renaming.
我不确定我能否解释导入错误,但我有三个想法。首先,您的功能需要调整。您使用了一个保留字'string'作为参数。考虑重命名。
Second, what happens if you invoke ./manage.py shell, and do the import by hand. Does it give you any different?
其次,如果您调用。/manage会发生什么。py shell,手工导入。有什么不同吗?
Third, try deleting your pyc files to force django to recompile python code (this one is a very long shot...but worth eliminating)
第三,尝试删除pyc文件以强制django重新编译python代码(这是一个很长的镜头……)但是值得消除)
#1
10
You are doing what is known as a Circular import.
您正在执行循环导入。
models.py:
models.py:
from bm.bmApp.utils import to_safe_uppercase
utils.py:
utils.py:
from bm.bmApp.models import Client
Now when you do import bm.bmApp.models
The interpreter does the following:
现在当你导入bmapp的时候。模型译员做以下工作:
-
models.py - Line 1
: try to importbm.bmApp.utils
- 模型。py -第1行:尝试导入bm.bmApp.utils
-
utils.py - Line 1
: try to importbm.bmApp.models
- 跑龙套。py - Line 1:尝试导入bm.bmApp.models
-
models.py - Line 1
: try to importbm.bmApp.utils
- 模型。py -第1行:尝试导入bm.bmApp.utils
-
utils.py - Line 1
: try to importbm.bmApp.models
- 跑龙套。py - Line 1:尝试导入bm.bmApp.models
- ...
- …
The easiest solution is to move the import inside the function:
最简单的解决方案是将导入移动到函数内部:
utils.py:
utils.py:
def get_client(user):
from bm.bmApp.models import Client
try:
client = Client.objects.get(username=user.username)
except Client.DoesNotExist:
print "User Does not Exist"
return None
else:
return client
def to_safe_uppercase(string):
if string is None:
return ''
return string.upper()
#2
7
You are creating a circular import.
您正在创建循环导入。
utils.py
from bm.bmApp.models import Client
# Rest of the file...
models.py
from bm.bmApp.utils import to_safe_uppercase
# Rest of the file...
I would suggest your refactor your code so that you don't have a circular dependency (i.e. utils should not need to import models.py or vice versa).
我建议您重构代码,这样就不会有循环依赖(例如,utils不需要导入模型)。py,反之亦然)。
#3
0
I'm not sure I can explain the Import error, but I have three ideas. First, your function needs tweaking. You've used a reserved word 'string' as an argument. Consider renaming.
我不确定我能否解释导入错误,但我有三个想法。首先,您的功能需要调整。您使用了一个保留字'string'作为参数。考虑重命名。
Second, what happens if you invoke ./manage.py shell, and do the import by hand. Does it give you any different?
其次,如果您调用。/manage会发生什么。py shell,手工导入。有什么不同吗?
Third, try deleting your pyc files to force django to recompile python code (this one is a very long shot...but worth eliminating)
第三,尝试删除pyc文件以强制django重新编译python代码(这是一个很长的镜头……)但是值得消除)