Jump to content

  • Log In with Google      Sign In   
  • Create Account


How to use the same system shell all the time in python


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Misery   Members   -  Reputation: 247

Like
0Likes
Like

Posted 18 April 2013 - 05:50 AM

Hi,

 

I need to run several shell commands from Python. How do I use all the time the same shell?

Because if I use  system in such manner:

 

 

from os import system

system("set x=0") #setting a system variable
system("echo %x%") #it does not exist here anymore
 

 

 

after first use the defined variable does not exist anymore.

How to workaround this?

 

Thanks,

 



Sponsor:

#2 Paradigm Shifter   Crossbones+   -  Reputation: 4782

Like
3Likes
Like

Posted 18 April 2013 - 06:43 AM

What happens if you call system once with a string containing newlines? If that works, build the complete string then execute it.


"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#3 ColinDuquesnoy   Members   -  Reputation: 948

Like
3Likes
Like

Posted 18 April 2013 - 06:51 AM

after first use the defined variable does not exist anymore.

 

When you call os.system, a new child process with a new environment is created. See this discussion for more details: http://stackoverflow.com/questions/1506579/why-doesnt-os-systemset-foo-bar-work

 

A workaround would be to separate your shell commands with new lines (\n) or write a bash that you run from your python script using the subprocess module.



#4 Misery   Members   -  Reputation: 247

Like
0Likes
Like

Posted 18 April 2013 - 11:19 AM

Thanks for answers.

Somehow this command with new line operator does not work, but one can join commands with & sign.



#5 ColinDuquesnoy   Members   -  Reputation: 948

Like
1Likes
Like

Posted 18 April 2013 - 11:46 AM

It works fine for me under Ubuntu 12.10, Python 2.7.3.

 

 What is the output of the following script?

 

import os
print os.system("x=15\necho $x")

 

I have the following output:

  

15
0

Edited by renega_666, 18 April 2013 - 11:48 AM.


#6 Paradigm Shifter   Crossbones+   -  Reputation: 4782

Like
1Likes
Like

Posted 18 April 2013 - 12:38 PM

If you are using Windows you might want to try \r\n instead of just \n for your newlines... perhaps that is the reason it doesn't work?


"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#7 Misery   Members   -  Reputation: 247

Like
0Likes
Like

Posted 19 April 2013 - 02:14 AM

As this what I am doing is a multiplatform installer I finally decided to use Batch/Shell files, so system command calls only a file which has everything inside. That's even better solution, than trying to concatenate all commands. It seems a bit more flexible now as one can put many scripts in a source directory and run them one by one.


Edited by Misery, 19 April 2013 - 02:15 AM.


#8 ColinDuquesnoy   Members   -  Reputation: 948

Like
0Likes
Like

Posted 19 April 2013 - 05:30 AM

Glad you found a solution that suits your needs.

 

May I ask you why you want to create a multiplatform installer? I ask this because it is usually advised to use the targeted system's package manager (Windows Installer on Windows, Debian and RPM on GNU/Linux, PyPi if you want to distribute a python package, and so on...)


Edited by renega_666, 19 April 2013 - 05:30 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS