簡體   English   中英

Python將在Unix而非Windows上運行。 如何在終端中運行外部python命令?

[英]Python will run in Unix, not Windows. How can you run an external python command in terminal?

我正在嘗試在iPython中運行一些代碼

will read in a tsv as a list,
go through it row-by row, 
    run an external python command on each line, 
    save the output into a list
append this to our tsv at the end.

我的代碼在Unix環境中運行,但是我無法使其在iPython和Windows中運行。 在Windows終端中調用時,我調用的外部函數也將起作用,但在iPython筆記本中如下所述被調用時,則不會。 我做錯了什么? 我還能嘗試什么?

這是我的代碼:

import csv
import GetAlexRanking #External Method exposed here
import subprocess
import pandas as p
loadData = lambda f: np.genfromtxt(open(f,'r'), delimiter=' ')
with open('list.tsv','rb') as tsvin, open('output.csv', 'wb') as csvout:
    tsvin = csv.reader(tsvin, delimiter='\t')
    csvout = csv.writer(csvout)

    for row in tsvin:
        count = 0
        url = str([row[count]])
        cmd = subprocess.Popen("python GetAlexRanking.py " + url ,shell=True)
        cmd_out, cmd_err = cmd.communicate()
        cmd_string = str(cmd_out)
        print ">>>>>>>URL : " + url + " " + cmd_string #testing
        csvout.writerows(url + "\t" + cmd_string) #writing
        count+=1

如何在Windows中使用iPython來運行此代碼? 目前,它沒有任何錯誤,但是cmd_out變量始終顯示“ None”而不是正確的值-python命令未正確運行(但是它將在正常的Python / Unix環境中運行)。

編輯:從Windows終端運行時,也可以正確運行。

采用

subprocess.Popen([sys.executable, "GetAlexRanking.py", url])

並最好將os.abspath("GetAlexRanking.py")作為參數傳遞。

>>> sys.executable
'C:\\Python27\\pythonw.exe'

可以在Windows,Linux和Mac上使用。

暫無
暫無

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

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