2

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

3
  • just import file1 in file2.py, I believe. There might be some weird directory stuff, but it should be fairly simple... Commented Jun 13, 2019 at 8:50
  • 1
    Why would you replace a question with several answers, with a completely different question ? Commented Jun 14, 2019 at 9:05
  • @austin I rollbacked your post to the original question. Please do not replace an existing question with something totally different, post a new question instead. Commented Jun 17, 2019 at 7:56

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Just be warned that rebinding token in file2 will not affect file1.token (but you shouldn't rebind nor mutate globals anyway).
OP just edited his question to be something completely different than what they originally asked -.-
0

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.

1 Comment

Please do not advise using wildcard imports (cf python.org/dev/peps/pep-0008/#imports)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.