Importing Variables from Another File

python using variables from another file

You can import the variables from the file:

vardata.py

verb_list = [x, y, z]
other_list = [1, 2, 3]
something_else = False

mainfile.py

from vardata import verb_list, other_list
import random

print random.choice(verb_list)

you can also do:

from vardata import *

to import everything from that file. Be careful with this though. You don't want to have name collisions.

Alternatively, you can just import the file and access the variables though its namespace:

import vardata
print vardata.something_else

Importing variables from another file?

from file1 import *  

will import all objects and methods in file1

How to import variables from another file AFTER the program has started in python?

You could just use

with open(file, "r") as f:
data = f.read().splitlines()

to read the data, assuming every variable is on its own line. This will read in all variables in the file file as a list called data containing each file line as one entry.

Note that this will read all variables in as strings, so you may have to cast to different types before actually using the values.

Import variables from another .py file

You should use any definitions from another python file like this:

import module
module.variable = 5
module.class()
module.function()

or You can do:

from module import variable, class, function
variable = 5
class()
function()

from module import * is not good practice.
If you hate typing, you can give another name:

from module import variable as var
var = 5

Importing variables from another python file

Assuming you've imported Proton, if you want it to work with minimal changes:

def testfall_db(self):

p = Proton() # need to create the instance first
p.test() # call the test method to initialize those values

# access those values from the `p` instance of Proton created above
self.curr.execute('''INSERT INTO testfall VALUES(?,?)''',(p.cases,p.links,))
self.curr.execute('''SELECT cases FROM testfall WHERE cases=?''', [p.cases])
self.conn.commit()

self.cases in your code for example, is calling the cases variable of the Database instance which does not exist -> None.

Importing variables from another file in Python

Everything you've done is right except when using the variables.

In your main_file.py file:

if(variables.flag == 0) :
variables.j = variables.j + 1

(Or)

Use the following header :

from variables import *

(Or)

from variables import flag, j

Replace all the references of flag and j (or any other variable you want to use from that file) with the prefix 'variables.'

Since this is just a copy of the variable, the values in the variables.py won't get affected if you modify them in main_file.py

Importing variables from a function inside another file

You've got a problem with scope. When you create a function, every variable you assign a value to is a local variable (unless you use the global keyword). You could use the global keyword like this :

mhealth=0
def common_monsterstat():
global mhealth
mattack = random.randint(1, 5)
mdefense = random.randint(0, 1)
mhealth = random.randint(5, 10)
mmaxhealth = health
def battlesys():
print("You challenged Monster!")
print("Monster's health: " + mhealth)
common_monsterstat()

This will make your code change the global health instead of creating a copy of it...

But the better way to do this is to simply return the health variable, because global keywords can become confusing after a while in my experience. However, if you need your game to be really efficient, which you probably don't, using global would save you from creating too many variables ( although not sure if it would be more efficient I just think so :)) So, you could do it like this:

def common_monsterstat():
mattack = random.randint(1, 5)
mdefense = random.randint(0, 1)
mhealth = random.randint(5, 10)
mmaxhealth = mhealth
return mhealth
def battlesys():
mhealth=common_monsterstat()
print("You challenged Monster!")
print("Monster's health: " + mhealth)
common_monsterstat()
battlesys()

This will create a localvariable, assign it a value, return it into another variable, and then print it... Seems more complex but is actually easier...

Tell me if you need any clarifications!
PS: You might wanna have a random name for your monster and use an f-string to name in a cooler way:

monsters=['centaurus','dwarf','spider']
def battlesys():
print(f"You challenged {r.choice(monsters)}!")
print("Monster's health: " + health)

PPS: you didn't import random in your code...



Related Topics



Leave a reply



Submit