Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8242703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:15:24+00:00 2026-06-07T21:15:24+00:00

Possible Duplicate: Global Variable from a different file Python is that possible to set

  • 0

Possible Duplicate:
Global Variable from a different file Python

is that possible to set variable in different file ?

So I have two different files somewhat like this..

tes1.py

import test2
st = 'a'
def main():
  mis = input('puts: ')
  print tes2.set(mis)
main()

tes2.py

def set(object):
  pics = ['''
  %s
  1st
  '''%st,
  '''
  %s
  2nd
  ''']
  return pics[object]

and i execute tes1.py and issued an error:

NameError: global name 'st' is not defined

thanks for the respond

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T21:15:26+00:00Added an answer on June 7, 2026 at 9:15 pm

    A few tips:

    • Wrap your main() function in a if __name__ == '__main__' block. It prevents imported modules from executing code that should not be run when being imported. I usually do away with main() completely and just write all my main code in the if block.
    • object is a built-in type. Don’t name things object. Also set, as that is a type as well.
    • Global variables are discouraged, as there is always a way to make global variables into parameters (unless your global variables are constants, which is fine).

    Here’s how I would do it:

    test1.py:

    import test2
    
    if __name__ == '__main__':
      st = 'a'
      mis = input('puts: ')
    
      print tes2.rename_me_set(st, mis)
    

    test2.py:

    def rename_me_set(st, obj):
      pics = ['''
      %s
      1st
      ''' % st,
      '''
      %s
      2nd
      ''']
    
      return pics[obj]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to programmatically set a global (module) variable? I have a class
Possible Duplicate: Python nested functions variable scoping After much trial and error I have
Possible Duplicate: Global int variable objective c I would like to create a global
Possible Duplicate: Reading and Writing Configuration Files Okay, I have a config file, which
Possible Duplicate: Android global variable I am working with an Android project. I have
Possible Duplicate: PHP: Global variable scope How can I set a variable outside a
Possible Duplicate: declaring a global dynamic variable in python >>> def f(): global cat
Possible Duplicate: Global variables in R ad the beginning of the file I wrote:
Possible Duplicate: PHP global in functions Using something like this worries me: <? global
Possible Duplicate: Extracting dollar amounts from existing sql data? I have a column in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.