sys.argv[0] always returns nothing [duplicate]





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







2
















This question already has an answer here:




  • Get the name of current script with Python

    12 answers




I was trying to use sys.argv[0] to get the name of the script but it returned nothing. I guess that was because no script name was passed to the Python interpreter but I had no idea how to fix it.



my code:



import sys
print ("This is the name of the script: ", sys.argv[0])
sys.argv[0]


outputs:



>>> import sys
>>> print ("This is the name of the script: ", sys.argv[0])
This is the name of the script:
>>> sys.argv[0]
''


Thank you










share|improve this question















marked as duplicate by cdlane, Stephen Rauch python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 2:36


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • There is no way to fix it, because there is nothing to fix? What were you hoping to make it return?

    – Paul Rooney
    Jan 4 at 2:41


















2
















This question already has an answer here:




  • Get the name of current script with Python

    12 answers




I was trying to use sys.argv[0] to get the name of the script but it returned nothing. I guess that was because no script name was passed to the Python interpreter but I had no idea how to fix it.



my code:



import sys
print ("This is the name of the script: ", sys.argv[0])
sys.argv[0]


outputs:



>>> import sys
>>> print ("This is the name of the script: ", sys.argv[0])
This is the name of the script:
>>> sys.argv[0]
''


Thank you










share|improve this question















marked as duplicate by cdlane, Stephen Rauch python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 2:36


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • There is no way to fix it, because there is nothing to fix? What were you hoping to make it return?

    – Paul Rooney
    Jan 4 at 2:41














2












2








2









This question already has an answer here:




  • Get the name of current script with Python

    12 answers




I was trying to use sys.argv[0] to get the name of the script but it returned nothing. I guess that was because no script name was passed to the Python interpreter but I had no idea how to fix it.



my code:



import sys
print ("This is the name of the script: ", sys.argv[0])
sys.argv[0]


outputs:



>>> import sys
>>> print ("This is the name of the script: ", sys.argv[0])
This is the name of the script:
>>> sys.argv[0]
''


Thank you










share|improve this question

















This question already has an answer here:




  • Get the name of current script with Python

    12 answers




I was trying to use sys.argv[0] to get the name of the script but it returned nothing. I guess that was because no script name was passed to the Python interpreter but I had no idea how to fix it.



my code:



import sys
print ("This is the name of the script: ", sys.argv[0])
sys.argv[0]


outputs:



>>> import sys
>>> print ("This is the name of the script: ", sys.argv[0])
This is the name of the script:
>>> sys.argv[0]
''


Thank you





This question already has an answer here:




  • Get the name of current script with Python

    12 answers








python read-eval-print-loop argv sys






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 4 at 2:41









Paul Rooney

12.8k72845




12.8k72845










asked Jan 4 at 2:33









XiaoyuXiaoyu

164




164




marked as duplicate by cdlane, Stephen Rauch python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 2:36


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by cdlane, Stephen Rauch python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 2:36


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • There is no way to fix it, because there is nothing to fix? What were you hoping to make it return?

    – Paul Rooney
    Jan 4 at 2:41



















  • There is no way to fix it, because there is nothing to fix? What were you hoping to make it return?

    – Paul Rooney
    Jan 4 at 2:41

















There is no way to fix it, because there is nothing to fix? What were you hoping to make it return?

– Paul Rooney
Jan 4 at 2:41





There is no way to fix it, because there is nothing to fix? What were you hoping to make it return?

– Paul Rooney
Jan 4 at 2:41












2 Answers
2






active

oldest

votes


















1














Well, that's well expected,



you're running your code on the interpreter, which is not any module nor file, so sys.argv knows that and gives you an empty string.



It's a good sign.



If you run it in an actual module or file, it will work perfectly, as expected.






share|improve this answer
























  • You're right. Thank you!

    – Xiaoyu
    Jan 4 at 3:18



















0














You should run your code in a shell, then the arguments fellow the python filename, just like: python3 test.py first second. then in the code, you can find the args in the file.



print(sys.argv[1]) -------> first<br>    
print(sys.argv[2]) -------> second


I hope it will be helpful.






share|improve this answer


























  • Test that and see what you get.

    – U9-Forward
    Jan 4 at 2:43











  • Thank you. I did run the code in a shell. But sys.argv[0] still can't get the file name for me. Both sys.argv[1] and sys.argv[2] return: IndexError: list index out of range. So I think sys.argv is still empty.

    – Xiaoyu
    Jan 4 at 2:53











  • @Xiaoyu You should see mine.

    – U9-Forward
    Jan 4 at 2:55











  • otherwise.you should take a screenshot,let me see.

    – franckisses
    Jan 4 at 9:16


















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Well, that's well expected,



you're running your code on the interpreter, which is not any module nor file, so sys.argv knows that and gives you an empty string.



It's a good sign.



If you run it in an actual module or file, it will work perfectly, as expected.






share|improve this answer
























  • You're right. Thank you!

    – Xiaoyu
    Jan 4 at 3:18
















1














Well, that's well expected,



you're running your code on the interpreter, which is not any module nor file, so sys.argv knows that and gives you an empty string.



It's a good sign.



If you run it in an actual module or file, it will work perfectly, as expected.






share|improve this answer
























  • You're right. Thank you!

    – Xiaoyu
    Jan 4 at 3:18














1












1








1







Well, that's well expected,



you're running your code on the interpreter, which is not any module nor file, so sys.argv knows that and gives you an empty string.



It's a good sign.



If you run it in an actual module or file, it will work perfectly, as expected.






share|improve this answer













Well, that's well expected,



you're running your code on the interpreter, which is not any module nor file, so sys.argv knows that and gives you an empty string.



It's a good sign.



If you run it in an actual module or file, it will work perfectly, as expected.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 4 at 2:35









U9-ForwardU9-Forward

18.1k51744




18.1k51744













  • You're right. Thank you!

    – Xiaoyu
    Jan 4 at 3:18



















  • You're right. Thank you!

    – Xiaoyu
    Jan 4 at 3:18

















You're right. Thank you!

– Xiaoyu
Jan 4 at 3:18





You're right. Thank you!

– Xiaoyu
Jan 4 at 3:18













0














You should run your code in a shell, then the arguments fellow the python filename, just like: python3 test.py first second. then in the code, you can find the args in the file.



print(sys.argv[1]) -------> first<br>    
print(sys.argv[2]) -------> second


I hope it will be helpful.






share|improve this answer


























  • Test that and see what you get.

    – U9-Forward
    Jan 4 at 2:43











  • Thank you. I did run the code in a shell. But sys.argv[0] still can't get the file name for me. Both sys.argv[1] and sys.argv[2] return: IndexError: list index out of range. So I think sys.argv is still empty.

    – Xiaoyu
    Jan 4 at 2:53











  • @Xiaoyu You should see mine.

    – U9-Forward
    Jan 4 at 2:55











  • otherwise.you should take a screenshot,let me see.

    – franckisses
    Jan 4 at 9:16
















0














You should run your code in a shell, then the arguments fellow the python filename, just like: python3 test.py first second. then in the code, you can find the args in the file.



print(sys.argv[1]) -------> first<br>    
print(sys.argv[2]) -------> second


I hope it will be helpful.






share|improve this answer


























  • Test that and see what you get.

    – U9-Forward
    Jan 4 at 2:43











  • Thank you. I did run the code in a shell. But sys.argv[0] still can't get the file name for me. Both sys.argv[1] and sys.argv[2] return: IndexError: list index out of range. So I think sys.argv is still empty.

    – Xiaoyu
    Jan 4 at 2:53











  • @Xiaoyu You should see mine.

    – U9-Forward
    Jan 4 at 2:55











  • otherwise.you should take a screenshot,let me see.

    – franckisses
    Jan 4 at 9:16














0












0








0







You should run your code in a shell, then the arguments fellow the python filename, just like: python3 test.py first second. then in the code, you can find the args in the file.



print(sys.argv[1]) -------> first<br>    
print(sys.argv[2]) -------> second


I hope it will be helpful.






share|improve this answer















You should run your code in a shell, then the arguments fellow the python filename, just like: python3 test.py first second. then in the code, you can find the args in the file.



print(sys.argv[1]) -------> first<br>    
print(sys.argv[2]) -------> second


I hope it will be helpful.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 4 at 6:50









Prashant Pokhriyal

2,36441825




2,36441825










answered Jan 4 at 2:41









franckissesfranckisses

11




11













  • Test that and see what you get.

    – U9-Forward
    Jan 4 at 2:43











  • Thank you. I did run the code in a shell. But sys.argv[0] still can't get the file name for me. Both sys.argv[1] and sys.argv[2] return: IndexError: list index out of range. So I think sys.argv is still empty.

    – Xiaoyu
    Jan 4 at 2:53











  • @Xiaoyu You should see mine.

    – U9-Forward
    Jan 4 at 2:55











  • otherwise.you should take a screenshot,let me see.

    – franckisses
    Jan 4 at 9:16



















  • Test that and see what you get.

    – U9-Forward
    Jan 4 at 2:43











  • Thank you. I did run the code in a shell. But sys.argv[0] still can't get the file name for me. Both sys.argv[1] and sys.argv[2] return: IndexError: list index out of range. So I think sys.argv is still empty.

    – Xiaoyu
    Jan 4 at 2:53











  • @Xiaoyu You should see mine.

    – U9-Forward
    Jan 4 at 2:55











  • otherwise.you should take a screenshot,let me see.

    – franckisses
    Jan 4 at 9:16

















Test that and see what you get.

– U9-Forward
Jan 4 at 2:43





Test that and see what you get.

– U9-Forward
Jan 4 at 2:43













Thank you. I did run the code in a shell. But sys.argv[0] still can't get the file name for me. Both sys.argv[1] and sys.argv[2] return: IndexError: list index out of range. So I think sys.argv is still empty.

– Xiaoyu
Jan 4 at 2:53





Thank you. I did run the code in a shell. But sys.argv[0] still can't get the file name for me. Both sys.argv[1] and sys.argv[2] return: IndexError: list index out of range. So I think sys.argv is still empty.

– Xiaoyu
Jan 4 at 2:53













@Xiaoyu You should see mine.

– U9-Forward
Jan 4 at 2:55





@Xiaoyu You should see mine.

– U9-Forward
Jan 4 at 2:55













otherwise.you should take a screenshot,let me see.

– franckisses
Jan 4 at 9:16





otherwise.you should take a screenshot,let me see.

– franckisses
Jan 4 at 9:16



Popular posts from this blog

Mossoró

Error while reading .h5 file using the rhdf5 package in R

Pushsharp Apns notification error: 'InvalidToken'