为什么我无法导入 Python 中的包?
Why I can not import the package in Python?
我使用这里的代码:
https://github.com/Jefferson-Henrique/GetOldTweets-python
并且每次我尝试导入文件夹时,import got
,它会引发:
Traceback (most recent call last):
File "C:\Users\USER\Desktop\python\get_old_tweet\Main.py", line 1, in <module>
import got
File "C:\Users\USER\Desktop\python\get_old_tweet\got\__init__.py", line 1, in <module>
import models
ImportError: No module named 'models'
我已经检查了文件并且非常确定他们确实有名为 models
的文件夹
并且该文件夹还包含__init__.py
个文件。
所以它应该运作良好..
我不知道它为什么不起作用。请帮助我!
这取决于您从哪里导入。在存储库中,您证明了 link 到 models
是 got
的子文件夹。试试这个:
from got import models
Python默认只搜索其默认库路径(包括运行脚本路径)。因此,您需要将它们放在 Python 默认库路径中或将模块路径附加到这些路径。
追加路径数组:
>>> import sys
>>> sys.path.append("path/to/the_module")
>>> import the_module
如果上述解决方案不起作用,请尝试:
>>> from models import got
您使用哪个版本的Python?
库https://github.com/Jefferson-Henrique/GetOldTweets-python是用Python2写的。
Python 2 和 Python 3 的导入行为略有不同:https://www.python.org/dev/peps/pep-0404/#imports
让我分享一下关于您的案例的导入示例:
$ python3
Python 3.5.0 |Anaconda 2.4.0 (x86_64)| (default, Oct 20 2015, 14:39:26)
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import got
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/viach/Downloads/GetOldTweets-python-master/got/__init__.py", line 1, in <module>
import models
ImportError: No module named 'models'
>>> ^C
KeyboardInterrupt
$ python2
Python 2.7.10 (default, Aug 22 2015, 20:33:39)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import got
因此,快速解决方案:在您的应用中使用 Python 2,这取决于 GetOldTweets-python
。
您可以使用 GetOldTweets 的 Python3 兼容分支-python:
https://github.com/Mottl/GetOldTweets-python3
我使用这里的代码: https://github.com/Jefferson-Henrique/GetOldTweets-python
并且每次我尝试导入文件夹时,import got
,它会引发:
Traceback (most recent call last):
File "C:\Users\USER\Desktop\python\get_old_tweet\Main.py", line 1, in <module>
import got
File "C:\Users\USER\Desktop\python\get_old_tweet\got\__init__.py", line 1, in <module>
import models
ImportError: No module named 'models'
我已经检查了文件并且非常确定他们确实有名为 models
并且该文件夹还包含__init__.py
个文件。
所以它应该运作良好..
我不知道它为什么不起作用。请帮助我!
这取决于您从哪里导入。在存储库中,您证明了 link 到 models
是 got
的子文件夹。试试这个:
from got import models
Python默认只搜索其默认库路径(包括运行脚本路径)。因此,您需要将它们放在 Python 默认库路径中或将模块路径附加到这些路径。
追加路径数组:
>>> import sys
>>> sys.path.append("path/to/the_module")
>>> import the_module
如果上述解决方案不起作用,请尝试:
>>> from models import got
您使用哪个版本的Python?
库https://github.com/Jefferson-Henrique/GetOldTweets-python是用Python2写的。 Python 2 和 Python 3 的导入行为略有不同:https://www.python.org/dev/peps/pep-0404/#imports
让我分享一下关于您的案例的导入示例:
$ python3
Python 3.5.0 |Anaconda 2.4.0 (x86_64)| (default, Oct 20 2015, 14:39:26)
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import got
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/viach/Downloads/GetOldTweets-python-master/got/__init__.py", line 1, in <module>
import models
ImportError: No module named 'models'
>>> ^C
KeyboardInterrupt
$ python2
Python 2.7.10 (default, Aug 22 2015, 20:33:39)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import got
因此,快速解决方案:在您的应用中使用 Python 2,这取决于 GetOldTweets-python
。
您可以使用 GetOldTweets 的 Python3 兼容分支-python:
https://github.com/Mottl/GetOldTweets-python3