链接Python文件助手

2024-10-03 04:29:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道如何实际链接python文件,但是,我不知道如何从这些链接的文件中获取变量。我试图抓住他们,但我一直得到的名字错误。在

我该怎么做?我想链接文件的原因是简单地整理我的脚本,而不是使它长1000000000行。另外,在导入的python脚本中,是否必须再次导入所有内容?另一个问题,我在使用其他脚本函数时是否使用self函数?在

ie

主脚本:

import sys, os
import importedpyfile

导入的Py文件

^{pr2}$

Tags: 文件函数importself脚本内容os链接
3条回答

I understand how to actually link python files, however, i don't understand how to get variable's from these linked files. I've tried to grab them and I keep getting NameError.

你怎么做到的?发布更多代码。例如,以下工作:

文件1.py

#!/usr/bin/env python

from file2 import file2_func, file2_variable

file2_func()
print file2_variable

文件2.py:

^{pr2}$

Also, in the imported python script, do i have to import everything again?

不,应该在python解释器读取该文件时导入模块。在

Another question, do i use the self function when using another scripts functions?

不,这通常是为了访问类成员。见python self explained。在

导入文件的方法也不止一种。请参阅另一个答案以了解一些解释。在

我想你想问的是如何从on.py文件访问全局变量,而不必处理名称空间。在

改为在cd1>中调用

from importedpyfile import *

理想情况下,你要保持现有的代码。但是,只需使用importedpyfile命名空间引用这些全局变量。在

例如

^{pr2}$

Python模块在C/C++链接库的意义上不是“链接”为可执行文件。Pythonimport操作创建一个引用导入模块的名称;没有这个名称,就没有(直接)访问另一个模块的方法。在

相关问题 更多 >