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

  • SEARCH
  • Home
  • 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 8252571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:35:49+00:00 2026-06-08T00:35:49+00:00

I am a newbie to python.I am trying to access a variable defined inside

  • 0

I am a newbie to python.I am trying to access a variable defined inside main() in a module which is being imported in the main function. I want a method to get this withput passing the deviceid variable to get_a()
main.py:-

global deviceid
import lib
deviceid=123
lib.get_a()

lib.py:-

def get_a():
    global deviceid
    prnit deviceid  

calling main or trying to access deviceid from python shell is returning

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "t.py", line 4, in pp
print a
NameError: global name 'deviceid' is not defined

I tried giving global deviceid in many places inside and outside module and function.Nothing helps.

somebody please help.

  • 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-08T00:35:50+00:00Added an answer on June 8, 2026 at 12:35 am

    There are no names in Python that are global to the entire program. Globals in Python are global to a particular module. To use a value in more than one module, you import its name from one module to another, making it available in two modules:

    # main.py
    import lib
    deviceid = 123
    lib.get_a()
    
    # lib.py
    import main
    def get_a():
        print main.deviceid
    

    As other commenters have pointed out, not using globals is probably a better option.

    # main.py
    import lib
    deviceid = 123
    lib.get_a(deviceid)
    
    # lib.py
    def get_a(deviceid):
        print deviceid
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Python newbie here. I'm trying to create a black jack game in which the
I'm a newbie in Python and I'm studying list comprehension. What I'm trying to
I am a newbie to Python and trying out different ways to optimize and
I am a newbie to python, and just few days back I started trying
I'm a total newbie trying to use Python to analyze my company's log files.
I am a Python and QT newbie. I am trying to do a little
Im my Python file, I have imported the win32gui module like this: import win32gui
is this correct way of doing.i am a newbie python class main(threading.Thread): queueLock =
I am a python newbie. i was trying out a django tutorial and got
I'm a newbie programmer trying to jump in to Python by building a script

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.