Python 从另一个文件导入变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17255737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:53:17  来源:igfitidea点击:

Importing variables from another file?

pythonfilevariablesimport

提问by Ofek

How can I import variables from one file to another?

如何将变量从一个文件导入到另一个文件?

example: file1has the variables x1and x2how to pass them to file2?

例如:file1有变量x1以及x2如何将它们传递给file2

How can I import allof the variables from one to another?

如何将所有变量从一个导入到另一个?

采纳答案by ennuikiller

from file1 import *  

will import all objects and methods in file1

将导入 file1 中的所有对象和方法

回答by Ashwini Chaudhary

Import file1inside file2:

导入file1内部file2

To import all variables from file1 without flooding file2's namespace, use:

要从 file1 导入所有变量而不淹没 file2 的命名空间,请使用:

import file1

#now use file1.x1, file2.x2, ... to access those variables

To import all variables from file1 to file2's namespace( not recommended):

将所有变量从 file1 导入到 file2 的命名空间(不推荐):

from file1 import *
#now use x1, x2..

From the docs:

文档

While it is valid to use from module import *at module level it is usually a bad idea. For one, this loses an important property Python otherwise has — you can know where each toplevel name is defined by a simple “search” function in your favourite editor. You also open yourself to trouble in the future, if some module grows additional functions or classes.

虽然from module import *在模块级别使用它是有效的,但通常是一个坏主意。首先,这失去了 Python 原本拥有的一个重要属性——您可以通过您最喜欢的编辑器中的简单“搜索”功能知道每个顶级名称的定义位置。如果某个模块增加了额外的函数或类,你将来也会遇到麻烦。

回答by Chris Redford

Best to import x1and x2explicitly:

最好明确导入x1x2

from file1 import x1, x2

This allows you to avoid unnecessary namespace conflicts with variables and functions from file1while working in file2.

这使您可以避免file1file2.

But if you really want, you can import allthe variables:

但如果你真的想要,你可以导入所有变量:

from file1 import * 

回答by Marc Rechté

Actually this is not really the same to import a variable with:

实际上这与导入变量并不完全相同:

from file1 import x1
print(x1)

and

import file1
print(file1.x1)

Altough at import time x1 and file1.x1 have the same value, they are not the same variables. For instance, call a function in file1 that modifies x1 and then try to print the variable from the main file: you will not see the modified value.

尽管在导入时 x1 和 file1.x1 具有相同的值,但它们不是相同的变量。例如,在 file1 中调用一个修改 x1 的函数,然后尝试从主文件中打印变量:您将看不到修改后的值。

回答by Lei Z

Marc responseis correct. Actually, you can print the memory address for the variables print(hex(id(libvar))and you can see the addresses are different.

马克的回答是正确的。实际上,您可以打印变量的内存地址print(hex(id(libvar)),您可以看到地址不同。

# mylib.py
libvar = None
def lib_method():
    global libvar
    print(hex(id(libvar)))

# myapp.py
from mylib import libvar, lib_method
import mylib

lib_method()
print(hex(id(libvar)))
print(hex(id(mylib.libvar)))

回答by Imran

In Pythonyou can access the contents of other files like as if they
are some kind of a library, compared to other languages like java or any oop base languages , This is really cool ;

Python您可以访问的其他文件的内容像,就好像它们
是某种库,比起像Java或任何OOP语言基础等语言,这是真的很酷;

This makes accessing the contents of the file or import it to to process it or to do anything with it ; And that is the Main reason why Pythonis highly preferred Language for Data Science and Machine Learning etc. ;

这使得访问文件的内容或将其导入以对其进行处理或对其进行任何操作;这就是为什么Python高度首选数据科学和机器学习等语言的主要原因;

And this is the picture of project structureThis

这是图片 project structure这个

Where I am accessing variables from .env filewhere the API linksand Secret keys reside .

我从.env file哪里访问变量API links和密钥所在的位置。

General Structure:

一般结构:

from <File-Name> import *

回答by Ravi G

script1.py

脚本1.py

title="Hello world"

script2.pyis where we using script1 variable

script2.py是我们使用 script1 变量的地方

Method 1:

方法一:

import script1
print(script1.title)

Method 2:

方法二:

from script1 import title
print(title)