簡體   English   中英

為什么我不能在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文件。

所以它應該運作良好。

我不知道它怎么不起作用。 請幫我!

您使用哪個版本的Python?

https://github.com/Jefferson-Henrique/GetOldTweets-python庫是使用Python 2編寫的。Python 2和Python 3的導入行為有些不同: https : //www.python.org/dev/peps/ PEP-0404 /#進口

讓我分享有關您的案例的導入示例:

$ 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

因此,為您提供快速解決方案 :在取決於GetOldTweets-python應用程序中使用Python 2。

這取決於您從哪里導入。 在存儲庫中,您證明了到models的鏈接是got的子文件夾。 嘗試這個:

from got import models

Python默認僅搜索其默認庫路徑(包括正在運行的腳本路徑)。 因此,您需要將它們放在Python默認庫路徑中,或將模塊路徑附加到這些路徑中。

追加路徑數組:

>>> import sys
>>> sys.path.append("path/to/the_module")
>>> import the_module

如果上述解決方案不起作用,請嘗試:

>>> from models import got

您可以使用GetOldTweets-python的與Python3兼容的fork:
https://github.com/Mottl/GetOldTweets-python3

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM