1

我在 Google Cloud Compute Instances 上使用了启动脚本:

setsid python home/junaid_athar/pull.py

在根目录登录时,我可以在 VM 上运行以下脚本而不会出现问题:

setsid python3 home/junaid_athar/btfx.py

但是,当我将 setid python3 home/junaid_athar/btfx.py 添加到启动脚本时,它会抛出一个错误:

ImportError: cannot import name 'opentype'

当我登录时,相同的脚本运行良好,但当我将它作为启动脚本运行时,为什么以及如何解决它?

更新:我对编程很陌生,并且破解了。这是脚本:

import logging
import time
import sys
import json
from btfxwss import BtfxWss
from google.cloud import bigquery

log = logging.getLogger(__name__)

fh = logging.FileHandler('/home/junaid_athar/test.log')
fh.setLevel(logging.CRITICAL)
sh = logging.StreamHandler(sys.stdout)
sh.setLevel(logging.CRITICAL)

log.addHandler(sh)
log.addHandler(fh)
    logging.basicConfig(level=logging.DEBUG, handlers=[fh, sh])

def stream_data(dataset_id, table_id, json_data):
    bigquery_client = bigquery.Client()
    dataset_ref = bigquery_client.dataset(dataset_id)
    table_ref = dataset_ref.table(table_id)
    data = json.loads(json_data)

# Get the table from the API so that the schema is available.
table = bigquery_client.get_table(table_ref)

rows = [data]
errors = bigquery_client.create_rows(table, rows)

wss=BtfxWss()
wss.start()

while not wss.conn.connected.is_set():
    time.sleep(2)

# Subscribe to some channels
wss.subscribe_to_trades('BTCUSD')

# Do something else
t = time.time()
while time.time() - t < 5:
    pass

# Accessing data stored in BtfxWss:
trades_q = wss.trades('BTCUSD')  # returns a Queue object for the pair.
while True:
    while not trades_q.empty():
        item=trades_q.get()
        if item[0][0]=='te':
            json_data={'SEQ':item[0][0], 'ID':item[0][1][0], 'TIMESTAMP':int(str(item[0][1][1])[:10]) , 'PRICE':item[0][1][3], 'AMOUNT':item[0][1][2], 'UNIQUE_TS':item[0][1][1], 'SOURCE':'bitfinex'}
            stream_data('gdax','btfxwss', json.dumps(json_data))
# Unsubscribing from channels:
wss.unsubscribe_from_trades('BTCUSD')

# Shutting down the client:
wss.stop()

我在标准 1-CPU 3.75mem 机器上运行它。(Debian GNU/Linux 9(延伸))。

我认为问题在于 python3 和模块的安装目录以及启动脚本的运行方式与登录到机器之间的区别——我该如何解决这个问题?

4

1 回答 1

5

弄清楚发生了什么。启动脚本作为(在?)根目录运行。我添加-u username到启动脚本的开头,它就像我通过 SSH 连接到服务器一样运行。一切都很好,谢谢大家的帮助!

于 2018-01-10T23:04:46.650 回答