Python Forum
Passing Variables between files.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Passing Variables between files.
#1
I have these two files
Main.py
# Script displays my organizations I have access to.
import os

os.chdir('/Users/virgi/Dropbox/Dropbox-Work/Wing Files/Development/Utils')

from menu_option import api_key
from menu_option import  organization_id

my_org =  dashboard.organizations.getOrganizations()

print(my_org)
choice.py
# script sets the correct environment variables
import meraki 

# Create the menu to be displayed
print('Please make a chose')
print("1 - For SandBox Virgil press")
print('2 - For SandBox Meraki')
print('3 - For LafageProduction')

user_input = int(input('Please select an enviroment: '))

# Determine the users input was and set variables
if user_input == 1:# Sandbox A
    api_key = 'ijkl'
    organization_id = '996719'
    print(api_key)
    

elif user_input == 2: # Sandbox B
    api_key = 'efgh' 
    organization_id = '566327653141842188'
    print(api_key)
    

elif user_input == 3:
    api_key = 'abcd' # Sandbox C
    organization_id = '647392446434509389'
    print(api_key)
else:
    print('Sorry there was not match')


# Creates the dashboard variable    
dashboard = meraki.DashboardAPI(api_key,output_log=False, print_console=False)
When I run the 2 scripts I get the api_key & organization_id but when I try to use the dashboard varialbe it says it isn't defined.

it works if I put all the code in a single code
# script sets the correct environment variables
import meraki 

# Create the menu to be displayed
print('Please make a chose')
print("1 - For SandBox Virgil press")
print('2 - For SandBox Meraki')
print('3 - For LafageProduction')

user_input = int(input('Please select an enviroment: '))

# Determine the users input was and set variables
if user_input == 1:# Sandbox A
    api_key = 'ijkl'
    organization_id = '996719'
    print(api_key)
    

elif user_input == 2: # Sandbox B
    api_key = 'efgh' 
    organization_id = '566327653141842188'
    print(api_key)
    

elif user_input == 3:
    api_key = 'abcd' # Sandbox C
    organization_id = '647392446434509389'
    print(api_key)
else:
    print('Sorry there was not match')


# Creates the dashboard variable    
dashboard = meraki.DashboardAPI(api_key,output_log=False, print_console=False)

my_org =  dashboard.organizations.getOrganizations()

print(my_org)
Reply
#2
please repost using code tags (python icon in toolbar).
Your indentation was removed when you posted without tags
Reply
#3
post the error code in proper BBCode tags too.
Recommended Tutorials:
Reply
#4
(Oct-16-2020, 09:00 PM)Larz60+ Wrote: please repost using code tags (python icon in toolbar).
Your indentation was removed when you posted without tags
Just some info,indentation will never be removed if user paste in with indentation in first place.
This is because i removed wysiwyg mode and any button to use it in editor.
Ask for repost can be okay,or can go in add code tag(now will see pasted in indentation) as i have done now.
Larz60+ likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  using variables with functions imported from different files. Scordomaniac 3 1,318 May-24-2022, 10:53 AM
Last Post: deanhystad
  How do use data from csv files as variables? JUSS1K 1 2,192 Oct-25-2020, 08:31 PM
Last Post: GOTO10
  Calling Variables from Other Files in Different Folders illmattic 14 5,656 Aug-01-2020, 07:02 PM
Last Post: deanhystad
  Python 2.7 passing variables from functions zetto33 1 1,809 Mar-19-2020, 07:27 PM
Last Post: Larz60+
  help with threading module and passing global variables ricardons 1 7,921 Feb-21-2019, 12:48 PM
Last Post: stullis
  Use Variables Generated from Functions in different files to use on the main file AykutRobotics 3 2,998 Jan-01-2019, 04:19 PM
Last Post: AykutRobotics
  Class Modules, and Passing Variables: Seeking Advice Robo_Pi 21 10,418 Mar-02-2018, 05:22 PM
Last Post: snippsat
  Passing Variables up and down through classes DavidC 2 3,537 May-09-2017, 01:30 AM
Last Post: DavidC
  Storing and using UI variables values across files xenas 5 4,328 Mar-24-2017, 11:23 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020