本文介绍了试图使用相对的进口,并打破了我的进口路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Mac上本地运行Flask服务器。



我的项目

 项目/my_lib/my_class.py 
project / testing / flask_server.py
project / testing / something / test_class.py

在某一点上,我尝试使用一些相对导入来测试不同目录中的类:

project / testing / something / test_class.py:

  from ..my_lib.my_class import MyClass 

这给了我一个错误:

  ValueError:试图相对导入超出顶层包

所以我退出了,但现在我无法让我的Flask服务器运行,即使我取消了新的导入代码。

  $ python testing / flask_server.py 

吐出来:

  Traceback(最近调用最后一个):
文件testing / flask_server.py,l ine 2,在< module>
from flask import Flask
文件/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/__init__.py,第17行,在<模块>
from werkzeug.exceptions import abort
文件/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/werkzeug/__init__.py,第154行,在<模块>
__import __('werkzeug.exceptions')
文件/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/werkzeug/exceptions.py,第71行,在< module>
from werkzeug.wrappers import Response
文件/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/werkzeug/wrappers.py,第26行, <模块>
from werkzeug.http import HTTP_STATUS_CODES,\
文件/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/werkzeug/http.py,行28,在< module>
from urllib.request import parse_http_list as _parse_list_header
ImportError:没有名为请求的模块

咦?




更新

瓶子导入错误只发生在最初调用错误代码的目录中。也就是说,如果我做的是从瓶子进口的瓶子

  Flask 

project / testing 目录中的任何位置,我得到导入错误,但是如果我在 project / 或者我的系统上的任何其他地方都没有问题..



$ hr
$ b

解决方案(部分):



我没有解释为什么发生这种情况,但是我做了以下的修改:


  1. 创建一个新的测试/ 目录,并将旧的文件复制到该目录中。旧的 testing / 目录必须被删除 - 它基本上已损坏。
  2. 我的跨目录是否使用绝对而不是相对路径导入。

(顺便说一句,我试图追溯我的步骤重现相对的进口错误,但无法,所以我不确定原因或解决方案的整个事情。)

解决方案

您可能有自己的 urllib2 python文件在你的系统路径中,也许在本地目录中。不要这样做,因为它会打破 werkzeug (和其他Python代码)。为了兼容python 2和3, werkzeug 使用如下构造:

  try:
from urllib2 import parse_http_list as _parse_list_header
except ImportError:#pragma:no cover $ b $ from urllib.request import parse_http_list as _parse_list_header

来自urllib2的将parse_http_list导入为_parse_list_header 行可能会导致 ImportError 例外如果你有一个本地的 urllib2.py 模块或者 urllib2 / __ init __。py 封装标准库文件。因为第一次导入会抛出一个 ImportError ,第二行被执行, >失败,因为 urllib.request 包仅在Python 3上可用。

从您的项目运行以下代码来诊断你在哪里 ave that module:

  import urllib2 
print urllib2 .__ file__

如果仍然有效,请执行:

  from urllib2 import parse_http_list as _parse_list_header 

因为它可能是 urllib2 间接导入了你被屏蔽的东西。 urllib2 从urlib import ... 语句使用,所以本地 urllib module也会中断导入。



重要的是你在你的flask项目中做这个 ,在 from flask import Flask line。


I'm running a Flask server locally on my Mac.

My project:

project/my_lib/my_class.py
project/testing/flask_server.py
project/testing/something/test_class.py

At one point, I tried to get fancy with some relative imports to test a class in a different directory:

In project/testing/something/test_class.py:

from ..my_lib.my_class import MyClass

That gave me an error:

ValueError: Attempted relative import beyond toplevel package

So I backed out of that, but now I can't get my Flask server to run, even though I eliminated the new import code.

$ python testing/flask_server.py

Spits out this:

Traceback (most recent call last):
  File "testing/flask_server.py", line 2, in <module>
    from flask import Flask
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/__init__.py", line 17, in <module>
    from werkzeug.exceptions import abort
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/werkzeug/__init__.py", line 154, in <module>
    __import__('werkzeug.exceptions')
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/werkzeug/exceptions.py", line 71, in <module>
    from werkzeug.wrappers import Response
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/werkzeug/wrappers.py", line 26, in <module>
    from werkzeug.http import HTTP_STATUS_CODES, \
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/werkzeug/http.py", line 28, in <module>
    from urllib.request import parse_http_list as _parse_list_header
ImportError: No module named request

Huh?


UPDATE:

The flask import error only happens in the directory the bad code was originally called from. That is, if I do

from flask import Flask

from anywhere in the project/testing dir, I get the import error, but if I do it in project/ or anywhere else on my system, it's fine..?


SOLUTION (PARTLY):

I don't have an explanation as to why this happened, but I did the following to fix it:

  1. Created a new testing/ directory and copied the files from the old one into it. The old testing/ directory had to be deleted- it was basically corrupted.
  2. Did my cross-directory imports using absolute instead of relative paths.

(By the way, I tried to retrace my steps to reproduce the relative import error but was unable to, so I'm not sure of either the cause or solution to this whole thing.)

解决方案

You probably have your own urllib2 python file in your system path, perhaps in the local directory. Don't do that, as it breaks werkzeug (and other python code).

To be compatible with both python 2 and 3, werkzeug uses constructs like:

try:
    from urllib2 import parse_http_list as _parse_list_header
except ImportError: # pragma: no cover
    from urllib.request import parse_http_list as _parse_list_header

The from urllib2 import parse_http_list as _parse_list_header line could throw a ImportError exception if you have a local urllib2.py module or urllib2/__init__.py package that masks the standard library file.

Because the first import throws an ImportError, the second line is executed, which also fails because the urllib.request package is only available on Python 3.

From your project, run the following code to diagnose where you have that module:

import urllib2
print urllib2.__file__

If that still works, then run:

from urllib2 import parse_http_list as _parse_list_header

as it could be that urllib2 indirectly imports something that you masked. urllib2 uses from urlib import ... statements for example, so a local urllib module would also break the import.

It is important that you do this from your flask project, just before the from flask import Flask line.

这篇关于试图使用相对的进口,并打破了我的进口路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 18:10