Python change variable in another file

If the "variable" you're referring to is an mutable value, what you're asking for will just work.

fileB:

my_variable = ["a list with a string in it"]

fileA:

from fileB import my_variable  # import the value
my_variable.append["and another string"]

After fileA has been loaded fileB.my_variable will have two values in it.

But, that only works for mutable values. If the variable is immutable, the code in fileA can't change it in place, and so you'll have issues. There's no way to directly fix that, but there are many ways to work around the issue and still get at what you want.

The easiest will simply be to use import fileB instead of from fileB import my_variable. This lets you anything in fileB's namespace, just by using a name like fileB.whatever. You can rebind things in the namespace to your heart's content:

fileB:

my_variable = 1    # something immutable this time

fileA:

import fileB
fileB.my_variable = 2   # change the value in fileB's namespace

That is probably the simplest approach.

Another solution would be to put the immutable variable inside a mutable container, and then modify the container, rather than the variable. For instance, if the string "a list with a string in it" was the value we wanted to change my the first example, we could simply assign a new value to my_variable[0] [rather than appending].

A common way to do this is to put values into a dictionary, list or even in a class [or a mutable instance of one]. Then you can import the container object and mutate it to change the value you care about.

Importing Variables

Welcome back to another blog post! This week, we’re going to take a break from all the SQL we’ve been doing. Instead, we’re going to run through some basics on importing variables from another Python file.

Importing Variables From Another File In The Same Directory

The simplest way to do so is to have a regular Python file that you want to import variables from, and the file you want to import the variable to, in the same directory.

Let’s say we have a file called testvariables.py, and variables a, b, and c:

testvariables.py

Now we want to import all these variables into another file/script. In order to do this, we can do the following:

This will import all the variables in the testvariables.py file.

If we want to choose specific variables to import, we can do so by indicating the variable names [that are in the file we want to import from]:

One thing to note is that if you have a defined variable with a name that’s the same as the variable you want to import, it will be overwritten after you import the variable. Like usual, Python will run from top to bottom, so the value that the variable takes depends on the order at which you define the variable and import a variable with the same name.

Importing A File With Spaces In The File Name

Things get a little more complicated when the file has spaces in its name. Let’s say our file is now called “test variables.py”. Since it has a space in its name, we would only get an error if we were to use the method we just described. Instead, we have to use the __import__ function with a syntax like the one shown below:

But in order to call the variable, we will need to specify the file name followed by a “.” and then the variable name:

Just like before, this also assumes that the file with the variables you want to import is in the same directory as the file you want to import the variables to.

Generally, you wouldn’t want your file names to have spaces and both of your files are in the same directory, since it’s not hard to do so and it’s good practice.

One good reason why this is helpful is if want to store a username, email, or password in a separate file so that you don’t leak your information from uploading a script that uses them.

We can also import functions that are defined in another file so that our script is much neater and easier to understand.

Hope this helped!

For a little bit of extra reading, you can check out some good and bad practices using the import function in the following link:

How do you change a variable to another file in Python?

A common way to do this is to put values into a dictionary, list or even in a class [or a mutable instance of one]. Then you can import the container object and mutate it to change the value you care about.

How do I share a variable between two files in Python?

The canonical way to share information across modules within a single program is to create a special module [often called config or cfg]. Import the config module in all modules of your application; the module then becomes available as a global name. In general, don't use from modulename import *.

Can you change a variable in Python?

Mutable and immutable types Some values in python can be modified, and some cannot. This does not ever mean that we can't change the value of a variable – but if a variable contains a value of an immutable type, we can only assign it a new value. We cannot alter the existing value in any way.

How do you write a variable to a text file in Python?

To write to a text file in Python, you follow these steps: First, open the text file for writing [or append] using the open[] function. Second, write to the text file using the write[] or writelines[] method. Third, close the file using the close[] method.

Chủ Đề