【问题标题】:Python: Passing variable from 1st script to 2nd script and passing different variable from 2nd script to 1st scriptPython:将变量从第一个脚本传递到第二个脚本,并将不同的变量从第二个脚本传递到第一个脚本
【发布时间】:2019-10-04 07:51:08
【问题描述】:

这些是脚本的简化版本。我仍在努力学习,所以他们是 很粗糙。

** movieInfo.py **

def castInfo():
    castMbrName = inputbox("Provide name")
    castPageURL = "https://www.cast_site.com/" + castMbrName
    # Most pages match this format. Some don't. For example: 
    #   "http://www.cast_site.com/" + castMbrFirstName + "-" + castMbrSecondName
    # or
    #   "http://www.cast_site.com/" + castMbrAliasName

    # To make sure I have the right page, I search for the cast member's name on it.
    sourceCastPage = requests.get(castPageURL, headers=hdr).text
    soupCastPage = BeautifulSoup(sourceCastPage, "lxml")
    try:
        castMbrNameOnPage = soupCastPage.find("div", attrs={"class":"xyz"}).text

        # If this succeeds, I use castPageURL in the rest of the script.
        cast_mbr_url = castPageURL
    except (AttributeError,TypeError):

        # If not, I want send castMbrName to castPageURL.py
        return castMbrName #<<see note below<<

        # Getting the value from castURL in castPageURL.url into movieInfo.py
        cast_mbr_url = checkCastNameURL()

castInfo()

# Do other things with cast_mbr_url
.
.
.

** castPageURL.py **

# This script will take the cast member's name, search the db and either add the name and url if not 
# found or return the url if found.

def checkCastNameURL():
    # This is a TinyDB setup containing the names and urls that don't match.
    db = TinyDB(castMbr_db.json)

    # I want to import the castMbrName variable from movieInfo.py into here.
    cast_name_req = castInfo()

    # When I manually provided the cast name, it worked. Now, I want to use castMbrName from movieInfo.py

    # Search the db for the cast member's name. If not found, get copy of correct url from site and add to db.
    if db.search(where("cast_name") == cast_name_req) == []:
    cast_name_url = simpledialog.askstring(
        title="",
        prompt="Paste cast member's url from site:",
        initialvalue="",
    ).strip()

    # Add name and url to db
    db.insert(
        {
            "cast_name": cast_name_req,
            "cast_url": cast_name_url,
        }
    )

    result = db.get(Query()["cast_name"] == castNameReq)
    castURL = result.get("cast_url")
    return castURL

我想将变量 castMbrName 从 movieInfo.py 发送到 castPageURL.py 并将 castURL 从 castPageURL.py 发送到 movieInfo.py。这些脚本单独工作,但我想将 castPageURL 合并到 movieInfo 中。

我的问题是我不知道在哪里导入什么。任何见解将不胜感激。

我试过了:

** movieInfo.py **

import castPageURL
From castPageURL import checkCastNameURL # with and without this

** castPageURL **

import movieInfo
from movieInfo import castInfo # with and without this as well

我也试过这个:

** movieInfo.py **

import castPageURL
From castPageURL import checkCastNameURL # with and without this

** castPageURL **

from __main__ import *

我什至尝试在函数内部而不是脚本顶部导入。

** movieInfo.py **

import castPageURL
From castPageURL import checkCastNameURL # with and without this

** castPageURL.py **

def checkCastNameURL():
    import movieInfo
    from movieInfo import castInfo # with and without this

我无法让这两个脚本识别对方的功能。

另外,在“try/except”块的“except”部分,一旦完成,

return castMbrName

如何让它运行?

cast_mbr_url = checkCastNameURL()

如果需要进一步说明,请告诉我。

【问题讨论】:

  • 对不起,这不是很容易理解。为什么你的函数会互相调用?他们之间不传递任何信息,那么为什么要这样做呢?究竟是什么问题;当您尝试这些导入时会发生什么?
  • 听起来您想将参数从一个函数传递到另一个函数。长期以来的习惯是“传递参数”,在您的情况下可能如下所示:cast_mbr_url = checkCastNameURL(castMbrName)
  • @DanielRoseman,我想将 cast_mbr 从第一个 py 脚本发送到第二个 py 脚本,看看它是否在数据库中。如果它不在数据库中,我想将 cast_mbr 和正确的 url 添加到数据库中,并将正确的 url 返回到第一个 py 脚本。如果 cast_mbr 在数据库中,我想从第二个 py 脚本返回数据库中的匹配 url,并将其发送到第一个 py 脚本以供稍后处理。我希望能澄清一些事情。

标签: python parameter-passing try-except


【解决方案1】:

如果要导入模块,请尝试从文件末尾删除castInfo() 并将其替换为

if __name__ == "__main__":
   castInfo()

这更好,因为它允许您手动运行方法,而不是在导入时运行。

同样,当导入时,语法是from castPageURL import checkCastNameURL,而不是From castPageURL import checkCastNameURL,这是一个容易犯的错误,但请记住python是区分大小写的。

关于你应该在哪里导入方法,你应该在程序的最开始就做。

如果你想从中导入,你不需要from castPageURL import checkCastNameURL之前的import castPageURL,但你需要使import castPageURL实际上是import path\to\castPageURL.py

【讨论】:

  • 我错过了导入中脚本的完整路径。感谢那。就这样我明白了,我这样做if __name__ == "__main__": 即使 castInfo() 不是要导入的函数,对吗? 来自 s/b 来自:抱歉,睡眠太少了。
  • 这样做是个好习惯,但是两者都应该工作。但是,它根本不应该改变程序的功能。
猜你喜欢
  • 1970-01-01
  • 2021-01-15
  • 1970-01-01
  • 2013-07-15
  • 2017-11-07
  • 2020-06-09
  • 1970-01-01
  • 1970-01-01
  • 2011-02-08
相关资源
最近更新 更多