-1

in python I have this set of variable

variable.py

#--------------Project 1--------------#
ip_server = '10.10.55.98'
username = 'user_1'
distro = 'debian'
#--------------Project 2--------------#
ip_server = '10.10.55.96'
username = 'user_2'
distro = 'opensuse'
#--------------Project 3--------------#
ip_server = '10.10.55.95'
username = 'user_3'
distro = 'ubuntu'

In the script main.py I just want to import variable of Project 2, how to this?

main.py

from variable import *
ho_to_import_variable_of_project2?

thanks to all for answers anche time to dedicate my question

Nimantha
6,5456 gold badges32 silver badges78 bronze badges
asked Jan 27, 2022 at 11:11
4
  • Does from variable import * not work for you? By the way, you are just redefining ip_server, username, and distro 3 times so at the end you will only have 3 variables, not 9. Commented Jan 27, 2022 at 11:14
  • 1
    You do not have separate variables for projects 1, 2, 3 – you overwrite the same variables each time, and at the end you only have the values for project 3. Commented Jan 27, 2022 at 11:16
  • Is your question how to import something (because you already seem to know that) or how to create different variables for each project? Commented Jan 27, 2022 at 11:17
  • Potential duplicate stackoverflow.com/questions/14573021/… Commented Jan 27, 2022 at 11:17

2 Answers 2

4

Your assumption that using comments to "separate" the variables to logical groups means anything to the interpreter is wrong. All you are really doing is overwriting the same variables.

Also, avoid using import *.

Instead, I'd use a dictionary:

data = {
 'Project 1': {
 'ip_server': '10.10.55.98',
 'username': 'user_1',
 'distro': 'debian'
 },
 'Project 2': {
 'ip_server': '10.10.55.96',
 'username': 'user_2',
 'distro': 'opensuse'
 },
 'Project 3': {
 'ip_server': '10.10.55.95',
 'username': 'user_3',
 'distro': 'ubuntu'
 }
}

then use it:

from variable import data
print(data['Project 1']['ip_server'])

will output

10.10.55.98

At this point you might as well use an external JSON file as a config file but that is behind the scope of this question/answer.

answered Jan 27, 2022 at 11:21
Sign up to request clarification or add additional context in comments.

Comments

0

Create a list and dictionary for each project like below:

variable.py
project1 = [{'ip_server' :'10.10.55.98', 'username':'user_1', 'distro' : 'debian'}]
project2 = [{'ip_server' :'10.10.55.95', 'username':'user_2', 'distro' : 'opensuse'}]
project3 = [{'ip_server' :'10.10.55.96', 'username':'user_3', 'distro' : 'ubuntu'}]

And in main.py

from variable import project1
answered Jan 27, 2022 at 11:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.