简体   繁体   English

导入 nltk package 时如何解决以下错误

[英]How can I solve the below error while importing nltk package

Screenshot of the error错误截图

After installing nltk using pip3 install nltk使用 pip3 install nltk 安装 nltk 之后

I am unable to import nltk in python shell in macOS我无法在 macOS 中的 python shell 中导入 nltk


  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/nltk/__init__.py", line 137, in <module>

    from nltk.text import *
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/nltk/text.py", line 29, in <module>

    from nltk.tokenize import sent_tokenize
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/nltk/tokenize/__init__.py", line 65, in <module>

    from nltk.tokenize.casual import TweetTokenizer, casual_tokenize
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/nltk/tokenize/casual.py", line 49, in <module>

    import regex  # https://github.com/nltk/nltk/issues/2409
  File "/Users/userId/Library/Python/3.9/lib/python/site-packages/regex/__init__.py", line 1, in <module>

    from .regex import *
  File "/Users/userId/Library/Python/3.9/lib/python/site-packages/regex/regex.py", line 419, in <module>

    import regex._regex_core as _regex_core
  File "/Users/userId/Library/Python/3.9/lib/python/site-packages/regex/_regex_core.py", line 21, in <module>

    import regex._regex as _regex
ImportError: dlopen(/Users/userId/Library/Python/3.9/lib/python/site-packages/regex/_regex.cpython-39-darwin.so, 2): no suitable image found.  Did find:
    /Users/userId/Library/Python/3.9/lib/python/site-packages/regex/_regex.cpython-39-darwin.so: code signature in (/Users/userId/Library/Python/3.9/lib/python/site-packages/regex/_regex.cpython-39-darwin.so) not valid for use in process using Library Validation: Trying to load an unsigned library```

Same error here using black library (MacOS M1 - python 3.9)使用black库时出现同样的错误(MacOS M1 - python 3.9)

    >>> import black
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/UserId/project/.venv/lib/python3.9/site-packages/black/__init__.py", line 13, in <module>
    import regex as re
  File "/Users/UserId/project/.venv/lib/python3.9/site-packages/regex/__init__.py", line 1, in <module>
    from .regex import *
  File "/Users/UserId/project/.venv/lib/python3.9/site-packages/regex/regex.py", line 419, in <module>
    import regex._regex_core as _regex_core
  File "/Users/UserId/project/.venv/lib/python3.9/site-packages/regex/_regex_core.py", line 21, in <module>
    import regex._regex as _regex
ImportError: dlopen(/Users/UserId/project/.venv/lib/python3.9/site-packages/regex/_regex.cpython-39-darwin.so, 2): no suitable image found.  Did find:
    /Users/UserId/project/.venv/lib/python3.9/site-packages/regex/_regex.cpython-39-darwin.so: code signature in (/Users/UserId/project/.venv/lib/python3.9/site-packages/regex/_regex.cpython-39-darwin.so) not valid for use in process using Library Validation: Trying to load an unsigned library

Just ran into this, I found that the following fixes it:刚遇到这个,我发现以下解决了它:

xcrun codesign --sign - "[YOUR_PATH_TO_DYLIB_HERE]"

In my case the error was like so:在我的情况下,错误是这样的:

ImportError: dlopen(/Users/USER/dev/cr-likes/venv/lib/python3.9/site-packages/regex/_regex.cpython-39-darwin.so, 2): no suitable image found.  Did find:
    /Users/USER/dev/cr-likes/venv/lib/python3.9/site-packages/regex/_regex.cpython-39-darwin.so: code signature in (/Users/USER/dev/cr-likes/venv/lib/python3.9/site-packages/regex/_regex.cpython-39-darwin.so) not valid for use in process using Library Validation: Trying to load an unsigned library

By running xcrun on the shared object, in this case /Users/USER/dev/cr-likes/venv/lib/python3.9/site-packages/regex/_regex.cpython-39-darwin.so the error is now gone.通过在共享对象上运行xcrun ,在这种情况下/Users/USER/dev/cr-likes/venv/lib/python3.9/site-packages/regex/_regex.cpython-39-darwin.so现在错误消失了.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM