这些是脚本的简化版本。我仍在学习中努力,所以他们 非常粗糙。
** 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
我想将来自movieInfo.py的变量castMbrName发送到castPageURL.py,并将来自castPageURL.py的castURL发送给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()
请让我知道是否需要进一步说明。
答案 0 :(得分:0)
如果要导入模块,请尝试从文件末尾删除castInfo()
并将其替换为
if __name__ == "__main__":
castInfo()
这更好,因为它允许您手动运行方法,而不是在导入时运行。
此外,从导入时,语法为from castPageURL import checkCastNameURL
,而不是From castPageURL import checkCastNameURL
,这是一个容易犯的错误,但请记住python区分大小写。
关于应在何处导入方法,应在程序开始时进行。
如果您要从中导入,则不需要在import castPageURL
之前插入from castPageURL import checkCastNameURL
,但是您需要这样做,以使import castPageURL
实际上是import path\to\castPageURL.py
< / p>