austin
austin

Reputation: 65

how to use variable data from one python file in another python file (python3.7)

file1.py

token="asdsadkj"

now I want to use that token in a new python file

file2.py

access=token      #data from file1.py
print(access)

output: asdsadkj

Upvotes: 2

Views: 57

Answers (2)

ToughMind
ToughMind

Reputation: 1009

You can just use import statement to import it. In python, any files with a .py extension is a Module that you can import. You can try:

from file1 import token

print(token)

or

# Wildcard imports (from <module> import *) should be avoided,
# as they make it unclear which names are present in the namespace,
# confusing both readers and many automated tools. See comment below.
from file1 import *

print(token)

or

import file1

print(file1.token)

For more details, you can refer to The import system. Also there is a tutorial about Modules and Packages.

Upvotes: 0

nagyben
nagyben

Reputation: 938

Assuming the files are in the same directory, i.e.

project
|-- file1.py
\-- file2.py
# file1.py
token = "asdsadkj"
# file2.py
from file1 import token

access = token
print(token)

Upvotes: 1

Related Questions