使用 Sublime 运行 Python 时出现“系统找不到指定的文件”错误

Posted

技术标签:

【中文标题】使用 Sublime 运行 Python 时出现“系统找不到指定的文件”错误【英文标题】:Error "The system cannot find the file specified" when running Python with Sublime 【发布时间】:2016-07-03 02:32:10 【问题描述】:

我尝试运行我的 twitter 机器人代码并收到此错误:

【错误2】系统找不到指定的文件 [cmd: [u'python', u'-u', u'C:\Users\humza\Desktop\Simple-Python-TwitterBot-master\run.py']] [目录:C:\Users\humza\Desktop\Simple-Python-TwitterBot-master] [路径:C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem ;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\Intel(R) 管理引擎组件\DAL;C:\Program Files (x86)\Intel\Intel(R) 管理引擎Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel \WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\RailsInstaller\Git\cmd;C:\Program Files\nodejs\;C:\Program Files (x86)\Heroku\bin ;C:\Program Files (x86)\git\cmd;C:\Program Files (x86)\Skype\Phone\;C:\Users\humza\AppData\Local\Programs\Common\Microsoft] 【已完结】

这是我的代码:

import tweepy
    from tweepy.auth import OAuthHandler
    from tweepy.streaming import StreamListener, Stream

    ckey = ""
    csecret = ""
    atoken = ""
    asecret = ""

    auths = OAuthHandler(ckey, csecret)
    auths.set_access_token(atoken, asecret)
    api = tweepy.API(auths)

    class listener(StreamListener):
        def on_data(self, raw_data):
            try:
                tweet_text = raw_data.lower().split('"text":"')        [1].split('","source":"')[0].replace(",", "")
                screen_name = raw_data.lower().split('"screen_name":"')[1].split('","location"')[0].replace(",", "")
                tweet_cid = raw_data.split('"id":')[1].split('"id_str":')[0].replace(",", "")

            accs = [] # banned account screen name goes in here
            words = [] # banned words goes in here

            if not any(acc in screen_name.lower() for acc in accs):
                if not any(word in tweet_text.lower() for word in words):
                    # call what u want to do here
                        #fav(tweet_cid)
                    #retweet(tweet_cid)
                    #syntax need to be fixed here
           return True

        except Exception as e:
            print(str(e)) # prints the error msg, if u dont want it comment it out
            pass


    def on_error(self, status_code):
        try:
            print( "error" + status_code)
        except Exception as e:
            print(str(e))
            pass

def create_tweet():
    """Kent is Great!"""
    # Replace this with your code!
    text = ""
    return text


def retweet(tweet_cid):
    try:
        api.retweet(tweet_cid)
    except Exception as e:
        print(str(e))
        pass


def fav(tweet_cid):
    try:
        api.create_favorite(tweet_cid)
    except Exception as e:
        print(str(e))
        pass


def unfav(tweet_cid):
    try:
        api.destroy_favorite(tweet_cid)
    except Exception as e:
        print(str(e))
        pass


def tweet(myinput):
    try:
        api.update_status(status=myinput)
    except Exception as e:
        print(str(e))
        pass

def tweet(text):
    """Send out the text as a tweet."""
    # Twitter authentication
    auth = tweepy.OAuthHandler(C_KEY, C_SECRET)
    auth.set_access_token(A_TOKEN, A_TOKEN_SECRET)
    api = tweepy.API(auth)

    # Send the tweet and log success or failure
    try:
        api.update_status(text)
    except tweepy.error.TweepError as e:
        log(e.message)
    else:
        log("Tweeted: " + text)


track_words = ["kent"]
follow_acc = ['946886204'] # all username converted to user ids

try:
    twt = Stream(auths, listener())
    twt.filter(track= track_words , follow = follow_acc)
except Exception as e:
    print(str(e))
    pass

【问题讨论】:

具体是如何运行的? 机器人代码似乎不太可能是相关的,因为问题在于无论您如何运行它找不到该代码 我正在尝试先在 Sublime 编辑器中运行它 【参考方案1】:

似乎 Python 不是您的环境变量 PATH 的一部分。

转到My Computer > Properties > Advanced > Environment Variables 并将"C:\python27;"(使用您的python 版本)添加到PATH 的开头,然后重新启动Sublime。

在 Sublime 中,使用“(ctrl-`)”打开控制台运行 os.getenv("PATH") 并验证 C:\python27; 是否在结果中。

【讨论】:

以上是关于使用 Sublime 运行 Python 时出现“系统找不到指定的文件”错误的主要内容,如果未能解决你的问题,请参考以下文章

使用 QT 运行 pytest 时出现致命的 Python 错误

尝试使用 python 3 运行 Spark 时出现几个错误

Sublime Text 安装插件时出现There are no packages available for installation解决步骤

在 Python 中使用 chromedriver_autoinstaller 运行 selenium 时出现问题

尝试运行可执行 Python 脚本时出现致命的 Python 错误

在 VSCode 中运行任何 Python 脚本时出现“&”语法错误?