如何从导入模块访问主要功能? [英] How do I access a main frunction from an import module?

查看:62
本文介绍了如何从导入模块访问主要功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我创建了一个导入模块。并希望从主脚本访问一个函数

,例如,


文件abc.py:

### ################

def a():

m()

返回无

####################

文件main.py:

#####################
来自abc import的
*

def m():

打印''东西''

返回无


a()

###### ################

python25.exe main.py


谢谢,

吉姆

Hi,

I have created an import module. And would like to access a function
from the main script, e.g.,

file abc.py:
###################
def a():
m()
return None
####################

file main.py:
#####################
from abc import *
def m():
print ''something''
return None

a()
######################

python25.exe main.py

Thanks,
Jim

推荐答案

Jim写道:
Jim wrote:
$ b $


我创建了一个导入模块。并希望从主脚本访问一个函数

,例如,


文件abc.py:

### ################

def a():

m()

返回无

####################

文件main.py:

#####################
来自abc import的
*

def m():

打印''东西''

返回无


a()

###### ################

python25.exe main.py
Hi,

I have created an import module. And would like to access a function
from the main script, e.g.,

file abc.py:
###################
def a():
m()
return None
####################

file main.py:
#####################
from abc import *
def m():
print ''something''
return None

a()
######################

python25.exe main.py



import __main__

....

__main __。m()

但更好的做一个start.py和import main - 然后它的对称性

Robert

import __main__
....
__main__.m()
but better make a start.py and "import main" - then its symmetric
Robert




Jim写道:

Jim wrote:

我创建了一个导入模块。并希望从主脚本访问一个函数

,例如,


文件abc.py:

### ################

def a():

m()

返回无

####################

文件main.py:

#####################
来自abc import的
*

def m():

打印''东西''

返回无


a()

###### ################
I have created an import module. And would like to access a function
from the main script, e.g.,

file abc.py:
###################
def a():
m()
return None
####################

file main.py:
#####################
from abc import *
def m():
print ''something''
return None

a()
######################



您可以使用from __main__ import m进行操作。在abc.py上面(在命令行调用的模块

总是被称为__main__)。


然而,我*高度*建议你把m放在另一个文件。从__main__导入

变量会使您的程序与许多

有用的工具不兼容。例如,如果你在

代码上调用Python分析器,就像这样:


python -m profile main.py

它会破坏你的代码,因为__main__不再引用main.py

而是引用profile.py(来自Python库)。从__main__导入

对PyChecker和PyLint等工具产生不利影响。


如果abc.py专门设计为

实用程序,用于交互式使用;那将是好的和有用的。

Carl Banks


You can do it with "from __main__ import m" atop abc.py (the module
invoked at the command line is always called __main__).

However, I *highly* suggest you put m in another file. Importing
variables from __main__ would make your program incompatible with many
useful tools. For example, if you invoke the Python profiler on your
code, like this:

python -m profile main.py

it will break your code, because __main__ no longer refers to main.py
but to profile.py (from the Python library). Importing from __main__
adversely affects tools such as PyChecker and PyLint.

The exception to this would be if abc.py is specifically designed as a
utility for interactive use; then it would be ok and useful.
Carl Banks


Jim写道:
Jim wrote:

我创建了一个导入模块。并希望从主脚本访问一个函数

,例如,


文件abc.py:

### ################

def a():

m()

返回无

####################

文件main.py:

#####################
来自abc import的
*

def m():

打印''东西''

返回无


a()
I have created an import module. And would like to access a function
from the main script, e.g.,

file abc.py:
###################
def a():
m()
return None
####################

file main.py:
#####################
from abc import *
def m():
print ''something''
return None

a()



import sys

def a():

sys.modules [''__ main __'']。m()

返回无


安东


''现在为什么有人想做那个*?''

import sys
def a():
sys.modules[''__main__''].m()
return None

Anton

''now why would anyone want to do *that* ?''

这篇关于如何从导入模块访问主要功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆