Jump to content

Importing variable from another file python

Wictorian

I wanna import a variable from another .py document on a python program. 

Should I import it at the top of the script or is it okay if I import it wherever I want?

Link to comment
Share on other sites

Link to post
Share on other sites

Import it at the top of the file. This allows anyone reading the file (including future you) to know what's been imported without needing to know you've randomly imported something in the middle of the file. It makes no functionality difference, and it prevents you from accidentally importing it twice.

¯\_(ツ)_/¯

 

 

Desktop:

Intel Core i7-11700K | Noctua NH-D15S chromax.black | ASUS ROG Strix Z590-E Gaming WiFi  | 32 GB G.SKILL TridentZ 3200 MHz | ASUS TUF Gaming RTX 3080 | 1TB Samsung 980 Pro M.2 PCIe 4.0 SSD | 2TB WD Blue M.2 SATA SSD | Seasonic Focus GX-850 Fractal Design Meshify C Windows 10 Pro

 

Laptop:

HP Omen 15 | AMD Ryzen 7 5800H | 16 GB 3200 MHz | Nvidia RTX 3060 | 1 TB WD Black PCIe 3.0 SSD | 512 GB Micron PCIe 3.0 SSD | Windows 11

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, BobVonBob said:

Import it at the top of the file. This allows anyone reading the file (including future you) to know what's been imported without needing to know you've randomly imported something in the middle of the file. It makes no functionality difference, and it prevents you from accidentally importing it twice.

I wanna import it according to user input so it actually makes functionality difference

like: 

var = input()

import var

So from your respond I understand I can import it wherever I want.

 

Link to comment
Share on other sites

Link to post
Share on other sites

13 hours ago, Wictorian said:

I wanna import it according to user input so it actually makes functionality difference

like: 


var = input()

import var

So from your respond I understand I can import it wherever I want.

 

So actually you can't quite do that. Imports can, but shouldn't, be used with variables and you need to use __import__ and assign that to a variable instead of normal import. Instead you can do this in Python 2.7 and 3, and because you've imported importlib you know that somewhere in the file a module will be imported.

 

import importlib	# top of the program

...			# rest of the program

moduleName = "math"	# get input
module = importlib.import_module(moduleName)
module.sqrt(4)

# or you can do this

from importlib import import_module

...

moduleName = "math" 	# get input again
module = import_module(moduleName)
module.sqrt(4)

 

The tricky part now is there's no way to allow the user to use functions from the module without using eval or exec, which can be dangerous for the rest of your program. (assuming you aren't making an interactive shell, in which case that would be the point)

¯\_(ツ)_/¯

 

 

Desktop:

Intel Core i7-11700K | Noctua NH-D15S chromax.black | ASUS ROG Strix Z590-E Gaming WiFi  | 32 GB G.SKILL TridentZ 3200 MHz | ASUS TUF Gaming RTX 3080 | 1TB Samsung 980 Pro M.2 PCIe 4.0 SSD | 2TB WD Blue M.2 SATA SSD | Seasonic Focus GX-850 Fractal Design Meshify C Windows 10 Pro

 

Laptop:

HP Omen 15 | AMD Ryzen 7 5800H | 16 GB 3200 MHz | Nvidia RTX 3060 | 1 TB WD Black PCIe 3.0 SSD | 512 GB Micron PCIe 3.0 SSD | Windows 11

Link to comment
Share on other sites

Link to post
Share on other sites

15 hours ago, BobVonBob said:

So actually you can't quite do that. Imports can, but shouldn't, be used with variables and you need to use __import__ and assign that to a variable instead of normal import. Instead you can do this in Python 2.7 and 3, and because you've imported importlib you know that somewhere in the file a module will be imported.

 


import importlib	# top of the program

...			# rest of the program

moduleName = "math"	# get input
module = importlib.import_module(moduleName)
module.sqrt(4)

# or you can do this

from importlib import import_module

...

moduleName = "math" 	# get input again
module = import_module(moduleName)
module.sqrt(4)

 

The tricky part now is there's no way to allow the user to use functions from the module without using eval or exec, which can be dangerous for the rest of your program. (assuming you aren't making an interactive shell, in which case that would be the point)

actually Im makşng kind of an interactive shell.

right now I am using pickle and storing data in 2 lists 

Link to comment
Share on other sites

Link to post
Share on other sites

6 hours ago, Wictorian said:

actually Im makşng kind of an interactive shell.

right now I am using pickle and storing data in 2 lists 

Very neat, I wish you luck.

¯\_(ツ)_/¯

 

 

Desktop:

Intel Core i7-11700K | Noctua NH-D15S chromax.black | ASUS ROG Strix Z590-E Gaming WiFi  | 32 GB G.SKILL TridentZ 3200 MHz | ASUS TUF Gaming RTX 3080 | 1TB Samsung 980 Pro M.2 PCIe 4.0 SSD | 2TB WD Blue M.2 SATA SSD | Seasonic Focus GX-850 Fractal Design Meshify C Windows 10 Pro

 

Laptop:

HP Omen 15 | AMD Ryzen 7 5800H | 16 GB 3200 MHz | Nvidia RTX 3060 | 1 TB WD Black PCIe 3.0 SSD | 512 GB Micron PCIe 3.0 SSD | Windows 11

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×