Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
Wictorian

Importing variable from another file python

Recommended Posts

Posted · Original PosterOP

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 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-3820 | Corsair H100i | ASUS P9X79-LE | 16GB Patriot Viper 3 1866MHz DDR3 | MSI GTX 970 Gaming 4G | 2TB WD Blue M.2 SATA SSD | 2TB Hitachi Deskstar HDD | 1TB WD Black HDD | Corsair CX750M Fractal Design Define R5 Windows 10 Pro / Linux Mint 20 Cinnamon

 

Laptop:

Dell XPS 15 9560 4K Touch | Intel Core i5-7300HQ | 12GB Generic (Crucial?) 2133MHz DDR4 | Nvidia GTX 1050 | 256GB Toshiba M.2 NVMe SSD | Windows 10

Link to post
Share on other sites
Posted · Original PosterOP
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 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-3820 | Corsair H100i | ASUS P9X79-LE | 16GB Patriot Viper 3 1866MHz DDR3 | MSI GTX 970 Gaming 4G | 2TB WD Blue M.2 SATA SSD | 2TB Hitachi Deskstar HDD | 1TB WD Black HDD | Corsair CX750M Fractal Design Define R5 Windows 10 Pro / Linux Mint 20 Cinnamon

 

Laptop:

Dell XPS 15 9560 4K Touch | Intel Core i5-7300HQ | 12GB Generic (Crucial?) 2133MHz DDR4 | Nvidia GTX 1050 | 256GB Toshiba M.2 NVMe SSD | Windows 10

Link to post
Share on other sites
Posted · Original PosterOP
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 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-3820 | Corsair H100i | ASUS P9X79-LE | 16GB Patriot Viper 3 1866MHz DDR3 | MSI GTX 970 Gaming 4G | 2TB WD Blue M.2 SATA SSD | 2TB Hitachi Deskstar HDD | 1TB WD Black HDD | Corsair CX750M Fractal Design Define R5 Windows 10 Pro / Linux Mint 20 Cinnamon

 

Laptop:

Dell XPS 15 9560 4K Touch | Intel Core i5-7300HQ | 12GB Generic (Crucial?) 2133MHz DDR4 | Nvidia GTX 1050 | 256GB Toshiba M.2 NVMe SSD | Windows 10

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


×