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 8299405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:08:30+00:00 2026-06-08T16:08:30+00:00

I am a python newbie. To pose my question in a comprehensible manner, I

  • 0

I am a python newbie. To pose my question in a comprehensible manner, I created two scripts. One script is named: called_script.py The other script is named: calling_script.py

The following two lines are the code in called_script.py

 import sys
 print str(sys.argv[1]) + '\n\n' + str(dir(sys.argv[1]))

The following two lines are the code in calling_script.py

 import sys
 import called_script

If I feed ‘foo’ as a command line argument to ‘calling_script.py’, foo will appear as sys.argv[1] in ‘called_script.py’

Is there any code that I could add to ‘called_script.py’ so that ‘called_script.py’ could ascertain
whether sys.argv[1] was passed to it from the command line, or whether sys.argv[1] was passed to it from ‘main‘?

Also, I am curious to know whether it is possible to prevent e.g., sys.argv[1] from being passed from main to an imported module, and where I could find some reading on this topic.

Thank you for any help. It is much appreciated.

                      Marc
  • 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-08T16:08:31+00:00Added an answer on June 8, 2026 at 4:08 pm

    Is there any code that I could add to ‘called_script.py’ so that
    ‘called_script.py’ could ascertain whether sys.argv[1] was passed to
    it from the command line, or whether sys.argv[1] was passed to it from
    ‘main’?

    Fundamentally, no, because command line arguments aren’t passed to modules, they’re passed to Python, which puts them in sys.argv. Since all the modules are sharing the same sys, they’ll all see the same sys.argv, and that’s that.

    That said, you can take advantage of __name__, which is set to __main__ only for the main script:

    ~/coding$ python calling_script.py fred
    called_script says __name__ is: called_script
    calling_script says __name__ is: __main__
    

    So you could use something like

    args = sys.argv if __name__ == '__main__' else [whatever_you_want]
    

    and then use args to get the equivalent of a module-specific sys.argv. Similarly, you could branch on __name__ at the very start of calling_script.py, stash sys.argv, and then modify the one in sys (ick).

    But generally speaking, you shouldn’t need to use sys.argv anywhere outside your main program anyway. If you need access to sys.argv in multiple places your interface probably isn’t separated enough from your routines.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

newbie Python question, 2.7.2 on Windows XP I saved a one-line hello.py file [print
Now following my series of python newbie questions and based on another question .
As a newbie to Python, and mainly having a background of writing scripts for
I'm a python newbie so I apologize in advance if this question has been
A clueless Python newbie needs help. I muddled through creating a simple script that
Python and django newbie question, here is code: class Client(User) #some fields client=Client() client.save()
I'm newbie in Python, but this question is not a homework (actually this code
Apologies, somewhat confused Python newbie question. Let's say I have a module called animals.py
A python newbie question: I need to do the following try: do-something() except error1:
I'm a Python newbie, so bear with me :) I created a file called

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.