导入自定义模块不起作用 (Python)

huangapple go评论78阅读模式
英文:

Importing custom modules doesn't work (Python)

问题

import Activities

文件目录应为Activities,Play,Football,然后是函数mcball。

例如:这个不起作用

Activities.play.football.mcball()

Activities、play和football都是文件,mcball是函数。

没有其他组合起作用,我尝试过。

即使使用"from ... import ... as ..."也不起作用

帮助

不起作用,不知道为什么。

另外,一切都是自定义的。 "Activities" 不是我从互联网上获取的模块。

英文:

I'm getting straight to the point:

import Activities
# File directory should be Activities,Play,Football then the function mcball.
# For example: This doesn't work
Activities.play.football.mcball()
# Activities, play, and football are files. mcball is the function.
# No other assortment works, I tried.
# Even "from ... Import ... as ..." doesn't work
#help

Doesn't work, no idea why.

Also, everything is custom. "Activities" is not a module i got from the internet.

答案1

得分: 2

如果您只导入顶层的Activities包,内部模块将不会自动加载,除非Activities/__init__.py文件为您导入它们。默认情况下,它不会执行任何操作,因此您需要导入实际的模块,使用以下代码:

import Activities.play.football

现在您应该能够成功调用Activities.play.football.mcball()

英文:

If you only import the top-level Activities package, the inner modules will not automatically be loaded, unless the Activities/__init__.py file imports them for you. By default, it won't do anything, so you need to import the actual module, with:

import Activities.play.football

Now you should be able to call Activities.play.football.mcball() successfully.

huangapple
  • 本文由 发表于 2023年2月9日 01:17:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75389416.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定