python的pyplot函数 (python怎么调用py文件中的函数)

本例子中被调用的py文件是百度翻译的API

获取API接口文档链接:https://ai.baidu.com/ai-doc/MT/4kqryjku9

python怎么调用py文件中的函数,python调用另一个py文件变量

完整代码:


# -*- coding: utf-8 -*-

# This code shows an example of text translation from English to Simplified-Chinese.
# This code runs on Python 2.7.x and Python 3.x.
# You may install `requests` to run this code: pip install requests
# Please refer to `https://api.fanyi.baidu.com/doc/21` for complete api document

import requests
import random
import json

def trans_baidu(query):
    token = '【调用鉴权接口获取的token】'
    url = 'https://aip.baidubce.com/rpc/2.0/mt/texttrans/v1?access_token=' + token
    
    q = '输入query'; # example: hello
    # For list of language codes, please refer to `https://ai.baidu.com/ai-doc/MT/4kqryjku9#语种列表`
    from_lang = '源语种方向'; # example: en
    to_lang = '目标语种方向'; # example: zh
    term_ids = ''; #术语库id,多个逗号隔开
    
    
    # Build request
    headers = {'Content-Type': 'application/json'}
    payload = {'q': q, 'from': from_lang, 'to': to_lang, 'termIds' : term_ids}
    
    # Send request
    r = requests.post(url, params=payload, headers=headers)
    result = r.json()
    
    # Show response
    print(json.dumps(result, indent=4, ensure_ascii=False))
    return r.json()

注意要设置成函数trans_baidu()

在另一个py文件中

#引入代码
import sys
from sys import path
path.append(sys.path[0]) #sys.path[0]是百度翻译.py所在文件夹地址
from Crawl.百度翻译 import trans_baidu
print(trans_baidu('黑色'))

path.append(sys.path[0])注意这个,要选择对自己的文件地址

正常返回

python怎么调用py文件中的函数,python调用另一个py文件变量