从模块访问全局命名空间 [英] Accessing global namespace from module

查看:60
本文介绍了从模块访问全局命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我是Python新手。我有以下问题/问题。

我有一个带命令行界面(CLI)的可视化软件,

本质上是一个Python(v.2.5)解释器函数

添加到全局命名空间。我想在一个单独的模块中保留我自己的函数

,然后将该模块导入主脚本

(将使用CLI解释器执行)。问题是,我的
无法从我的模块访问主脚本的全局命名空间中的函数

。反正有没有这样做?


这是我的意思的一个例子。函数AddPlot()和

DrawPlots()由软件CLI添加到全局命名空间。如果

我这样做:


mainscript.py:

------------- --------------

AddPlot(" scatter"," coordinate")

#在这里设置其他内容
DrawPlots()


它工作正常。但我希望能够做到这一点:


myModule.py:

---------------- ------

def defaultScatterPlot():

AddPlot(" scatter"," coordinate")

#do其他东西

DrawPlots()


然后在mainscript.py中:

----------- ----------------------------

导入myModule

myModule.defaultScatterPlot( )


这不行,因为myModule.py无法访问AddPlot()。

如何做这样的事情?


提前感谢您的帮助。

RDB

解决方案

6月11日上午11:02,reubendb< reube ... @ gmail.comwrote:


您好,

我是Python的新手。我有以下问题/问题。

我有一个带命令行界面(CLI)的可视化软件,

本质上是一个Python(v.2.5)解释器函数

添加到全局命名空间。我想在一个单独的模块中保留我自己的函数

,然后将该模块导入主脚本

(将使用CLI解释器执行)。问题是,我的
无法从我的模块访问主脚本的全局命名空间中的函数

。反正有没有这样做?


这是我的意思的一个例子。函数AddPlot()和

DrawPlots()由软件CLI添加到全局命名空间。如果

我这样做:


mainscript.py:

------------- --------------

AddPlot(" scatter"," coordinate")

#在这里设置其他内容
DrawPlots()


它工作正常。但我希望能够做到这一点:


myModule.py:

---------------- ------

def defaultScatterPlot():

AddPlot(" scatter"," coordinate")

#do其他东西

DrawPlots()


然后在mainscript.py中:

----------- ----------------------------

导入myModule

myModule.defaultScatterPlot( )


这不行,因为myModule.py无法访问AddPlot()。

如何做这样的事情?


提前感谢您的帮助。

RDB



我想你在做向后。如果你想访问AddPlot,那么

你应该将mainscript导入该模块而不是另一种方式

。当我想要从不同的

脚本调用常用方法时,我将这些方法/函数放入他们自己的模块/文件中。

然后我只导入模块并调用我需要的任何脚本。


< code>


commonMods.py

------ ---------------

AddPlot(* args,* kwargs):

#做点什么

DrawPlots(* args,* kwargs):

#做点什么

---------------------

mainProgram.py

------------------------

来自commonMods导入AddPlot

AddPlot(" scatter"," coordinate")

#etc等

------ -------------------

myModule.py

-------- ----------------

来自commonMods导入AddPlot

AddPlot(" scatter"," coordinate")

#etc等

-------------------------


< / code>


希望有所帮助。


Mike


6月11日下午1点37分,kyoso ... @ gmail.com写道:


6月11日上午11:02,reubendb< reube ... @ gmail.comwrote:


你好,

我是Python新手。我有以下问题/问题。

我有一个带命令行界面(CLI)的可视化软件,

本质上是一个Python(v.2.5)解释器函数

添加到全局命名空间。我想在一个单独的模块中保留我自己的函数

,然后将该模块导入主脚本

(将使用CLI解释器执行)。问题是,我的
无法从我的模块访问主脚本的全局命名空间中的函数

。反正有吗?



< snip>


我认为你是倒退了。如果你想访问AddPlot,那么

你应该将mainscript导入该模块而不是另一种方式

。当我想要从不同的

脚本调用常用方法时,我将这些方法/函数放入他们自己的模块/文件中。

然后我只导入模块并调用我需要的任何脚本。


< code>


commonMods.py

------ ---------------

AddPlot(* args,* kwargs):

#做点什么

DrawPlots(* args,* kwargs):

#做点什么

---------------------



嗨Mike,

问题是我没有定义函数AddPlot()和DrawPlots()。

它内置于我提到的

程序的CLI版本的python解释器中,它们是在主脚本上定义的。我使用类似software -cli -s

mainscript.py之类的东西加载

主脚本。

在mainscript.py中我导入myModule,但当然myModule确实没有访问
mainscript.py的全局命名空间中定义的函数。


谢谢。

RDB


En Mon,2007年6月11日15:18:58 -0300,reubendb< re ******@gmail.comescribió:


问题是我没有定义函数AddPlot()和DrawPlots()。

它内置于我提到的

程序的CLI版本的python解释器中,它们是在主脚本上定义的。我使用类似software -cli -s

mainscript.py之类的东西加载

主脚本。

在mainscript.py中我导入myModule,但当然myModule确实无法访问
mainscript.py的全局命名空间中定义的函数。



不要在mainscript.py顶部有一些导入语句

负责将AddPlot和DrawPlots带入当前命名空间?

在第二个模块中导入相同的内容。


-

Gabriel Genellina


Hello,
I am new to Python. I have the following question / problem.
I have a visualization software with command-line interface (CLI),
which essentially is a Python (v. 2.5) interpreter with functions
added to the global namespace. I would like to keep my own functions
in a separate module and then import that module to the main script
(that will be executed using the CLI interpreter). The problem is, I
cannot access the functions in the global namespace of the main script
from my module. Is there anyway to do that ?

Here is an example of what I meant. The function AddPlot() and
DrawPlots() are added to the global namespace by the software CLI. If
I do this:

mainscript.py:
---------------------------
AddPlot("scatter", "coordinate")
# set other things here
DrawPlots()

it works fine. But I want to be able to do this:

myModule.py:
----------------------
def defaultScatterPlot():
AddPlot("scatter", "coordinate")
#do other things
DrawPlots()

and then in mainscript.py:
---------------------------------------
import myModule
myModule.defaultScatterPlot()

This won''t work because myModule.py doesnot have access to AddPlot().
How do I do something like this ?

Thank you in advance for any help.
RDB

解决方案

On Jun 11, 11:02 am, reubendb <reube...@gmail.comwrote:

Hello,
I am new to Python. I have the following question / problem.
I have a visualization software with command-line interface (CLI),
which essentially is a Python (v. 2.5) interpreter with functions
added to the global namespace. I would like to keep my own functions
in a separate module and then import that module to the main script
(that will be executed using the CLI interpreter). The problem is, I
cannot access the functions in the global namespace of the main script
from my module. Is there anyway to do that ?

Here is an example of what I meant. The function AddPlot() and
DrawPlots() are added to the global namespace by the software CLI. If
I do this:

mainscript.py:
---------------------------
AddPlot("scatter", "coordinate")
# set other things here
DrawPlots()

it works fine. But I want to be able to do this:

myModule.py:
----------------------
def defaultScatterPlot():
AddPlot("scatter", "coordinate")
#do other things
DrawPlots()

and then in mainscript.py:
---------------------------------------
import myModule
myModule.defaultScatterPlot()

This won''t work because myModule.py doesnot have access to AddPlot().
How do I do something like this ?

Thank you in advance for any help.
RDB

I think you''re doing it backwards. If you want access to AddPlot, then
you should import mainscript into that module instead of the other way
around. When I have common methods I want to call from different
scripts, I put those methods/functions into their own module/file.
Then I just import the module and call whatever script I need.

<code>

commonMods.py
---------------------
AddPlot(*args, *kwargs):
# Do something
DrawPlots(*args, *kwargs):
# Do something
---------------------
mainProgram.py
------------------------
from commonMods import AddPlot
AddPlot("scatter", "coordinate")
# etc etc
-------------------------

myModule.py
------------------------
from commonMods import AddPlot
AddPlot("scatter", "coordinate")
# etc etc
-------------------------

</code>

Hope that helps.

Mike


On Jun 11, 1:37 pm, kyoso...@gmail.com wrote:

On Jun 11, 11:02 am, reubendb <reube...@gmail.comwrote:

Hello,
I am new to Python. I have the following question / problem.
I have a visualization software with command-line interface (CLI),
which essentially is a Python (v. 2.5) interpreter with functions
added to the global namespace. I would like to keep my own functions
in a separate module and then import that module to the main script
(that will be executed using the CLI interpreter). The problem is, I
cannot access the functions in the global namespace of the main script
from my module. Is there anyway to do that ?

<snip>

I think you''re doing it backwards. If you want access to AddPlot, then
you should import mainscript into that module instead of the other way
around. When I have common methods I want to call from different
scripts, I put those methods/functions into their own module/file.
Then I just import the module and call whatever script I need.

<code>

commonMods.py
---------------------
AddPlot(*args, *kwargs):
# Do something
DrawPlots(*args, *kwargs):
# Do something
---------------------

Hi Mike,
The problem is I don''t define the functions AddPlot() and DrawPlots().
It''s built into the python interpreter of the CLI version of the
program I mentioned, and they are defined on the main script. I load
the main script using something like "software -cli -s
mainscript.py".
In the mainscript.py I import myModule, but of course myModule does
not have access to the functions defined in the global namespace of
mainscript.py.

Thanks.
RDB


En Mon, 11 Jun 2007 15:18:58 -0300, reubendb <re******@gmail.comescribió:

The problem is I don''t define the functions AddPlot() and DrawPlots().
It''s built into the python interpreter of the CLI version of the
program I mentioned, and they are defined on the main script. I load
the main script using something like "software -cli -s
mainscript.py".
In the mainscript.py I import myModule, but of course myModule does
not have access to the functions defined in the global namespace of
mainscript.py.

Don''t you have some import statements at the top of mainscript.py that are
responsible for bringing AddPlot and DrawPlots into the current namespace?
Import the same things in your second module.

--
Gabriel Genellina


这篇关于从模块访问全局命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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