简体   繁体   English

在我的PC上正常工作的Python代码在我的Raspberry Pi上不起作用

[英]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:似乎它与硬编码的变量textplayer 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.

相关问题 Raspberry Pi:如果使用lxterminal自动启动,Python脚本不起作用,但是如果使用geany启动,则可以正常工作。 我的错误可能是什么? - Raspberry Pi: Python Script doesn't work if autostarted with lxterminal but if started with geany it works just fine. What could be my mistake? 如果我通过我的电脑启动它,我的 heroku 音乐机器人工作正常,但是当我通过 heroku 运行它时它不起作用 - My heroku music bot works fine if i launch it through my pc yet when i run it through heroku it doesn't work 我的 python 代码在操场上有效,但在 python 中无效 - My python code works on playground, but doesn't work in python 我的(工作)树莓派程序在启动时运行不正常 - My (working) raspberry pi program doesn't work right when I run it on boot 使用 python 和 QML 示波器动态绘图效果很好,但相同的程序在树莓派中不起作用,更换 function 不起作用 - Graphing dynamically with python and QML Oscilloscope works great, but the same program doesn't work in raspberry pi, replace function don't work 在 Raspberry Pi 上的 python 中获取 CPU 负载不起作用 - Get CPU load in python on Raspberry Pi doesn't work Raspberry PI 3 GPIO 不起作用 - Python3 - Raspberry PI 3 GPIO doesn't work - Python3 Python 代码不执行。 没有显示错误(树莓派) - Python code doesn't execute. No errors showed (raspberry pi) 我的python代码不起作用 - My python code doesn't work 在树莓派上的 Visual Studio 代码中使用我自己的 python 库 - Use my own python libraries in visual studio code on raspberry pi
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM