【问题标题】:Python standard library not working inside virtualenvPython 标准库在 virtualenv 中不起作用
【发布时间】:2014-11-04 04:55:20
【问题描述】:

这里是re.py的内容:

import re
if re.search('test', 'test'): print 'match'

当我运行$ python re.py 时,输出显然是match。但是当我激活 virtualenv 并尝试再次运行脚本时,我得到:

...
    if re.search('test', 'test'): print 'match'
AttributeError: 'module' object has no attribute 'search'

以下是 virtualenv 未激活时 Python 解释器的输出:

$ python
Python 2.7.5 (default, Jun  3 2013, 17:42:22)
[GCC 4.2.1 Compatible Apple Clang 4.0 ((tags/Apple/clang-421.0.57))] on darwin

这是激活时的输出:

$ python
Python 2.7.6 (default, Sep  9 2014, 15:04:36)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin

为什么在 virtualenv 中使用 Python 2.7.5 时 re 模块会中断?

【问题讨论】:

    标签: python regex python-2.7 virtualenv virtualenvwrapper


    【解决方案1】:

    问题是re内部模块和re.py你的模块混合在一起。

    请将您的文件名更改为re_example.py 并尝试。

    【讨论】:

    • 那行得通——谢谢。但是为什么 virtualenv 很重要呢?
    • 我必须检查,但永远不要将文件名作为 python 内置模块。
    猜你喜欢
    • 2018-06-09
    • 2016-03-22
    • 2017-08-03
    • 2013-04-20
    • 1970-01-01
    • 2015-03-29
    • 2015-07-06
    • 1970-01-01
    • 2018-01-10
    相关资源
    最近更新 更多