[英]Python code that works fine on my PC doesn't work on my Raspberry Pi
I recently bought a raspberry pi for many projects, one of which is reddit bots. 我最近为许多项目买了一个覆盆子pi,其中一个是reddit机器人。 I'm using PRAW to make the bot and it works perfectly on my desktop PC(Windows 8.1) but on my raspberry by (Raspbian) it doesn't seem to work.
我正在使用PRAW制作机器人,它在我的桌面PC(Windows 8.1)上完美运行,但在我的覆盆子(Raspbian)上它似乎不起作用。 I've narrowed it down to these few lines in question.
我把它缩小到这几行。
Brief explanation of what I'm trying to do: Get the selftext of the post and split it by spaces into words then check if certain words are in the post. 我正在尝试做的简要解释:获取帖子的自我文本并将其用空格分成单词然后检查某些单词是否在帖子中。
text = submission.selftext.replace(","," ").encode("utf-8").lower().split()
for i in range (0, len(players)):
player = players[i].lower()
if player in text:
print(player)
On my PC this works fine but on the Pi the if statement was never triggered even though I'm using the EXACT SAME POST for each of these tests. 在我的电脑上,这工作正常,但在Pi上if语句从未被触发,即使我正在为每个测试使用EXACT SAME POST。
If you are not familiar with PRAW, I'm splitting a string and looking for words inside it, where text
is the array of words to look through and players
is the dictionary. 如果你不熟悉PRAW,我拆分字符串,并寻找里面的话,这里
text
是通过词看起来和数组players
是字典。
Edit: The code does not produce any errors, it just doesn't trigger the if statement like it does on my desktop. 编辑:代码不会产生任何错误,它只是不像我在桌面上那样触发if语句。
Edit #2: Seems that it works fine with hard-coded variable text
and player
. 编辑#2:似乎它与硬编码的变量
text
和player
。 I left text hard-coded to include an entry from what should be in players
but it would not find it which leads me to believe the error is in how I create the list players
Below is the method that I use to create it. 我保留了硬编码的文本,以包含来自
players
内容的条目,但它不会发现它让我相信错误在于我如何创建列表players
以下是我用来创建它的方法。
def getPlayers():
players = []
with open("PlayerIDs.txt") as f:
for line in f:
players.append(line)
return players
SOLUTION between Python 2 and 3 there were changes to how unicode and encoding in general worked and therefor the code would not run on python 2.x on my pi. Python 2和3之间的解决方案是unicode和编码一般工作的变化,因此代码不能在我的pi上的python 2.x上运行。 I ended up installing python3 and using virtualenv to setup python 3 and it all works fine now :)
我最终安装了python3并使用virtualenv来设置python 3,现在一切正常:)
One possible issue is that you are running different versions of Python. 一个可能的问题是您运行的是不同版本的Python。 Try running the following code on both the raspi and the computer:
尝试在raspi和计算机上运行以下代码:
print (sys.version)
I'll bet that one will say "2.something" and one will say "3.something". 我敢打赌,人们会说“2.something”,而且会说“3.something”。 This, if it happens, means that you are using incompatible-ish versions of Python.
如果它发生,这意味着您使用的是不兼容的Python版本。 The pi comes with python 2 (the IDE with it is IDLE by default, and IDLE 3, an IDE for Python 3 also is preloaded in rasbian. Just run the script in the IDE that matches the version on your computer.
pi带有python 2(默认情况下IDE带有IDLE,而ID3 3,用于Python 3的IDE也是用rasbian预装的。只需在IDE中运行与您计算机上的版本匹配的脚本。
Also, I know that it sounds simple, but the pi has often unreliable network capabilities. 此外,我知道这听起来很简单,但pi通常具有不可靠的网络功能。 Are you sure that is connecting to the Internet properly?
您确定正确连接到Internet吗?
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.