running python script in interactive python prompt and keep the variables? [duplicate] - python

This question already has answers here:
How to execute a file within the Python interpreter?
(12 answers)
Closed 5 years ago.
I understand from How do I run a Python program? that in command prompt i can use
C:\python>python first.py
, to run first.py.
But, is it possible, that after i entered the interactive python prompt, by runnning
C:\python>python
and see the >>> python indication, run first.py, and after finished running first.py, back to the interactive python prompt, I could see variables defined inside first.py?
For example, if first.py created some variables inside, e.g. by
(x,y) = [3,5]
, is it possible that after running first.py and back to the interactive python prompt, x and y are still there?
Running windows shell commands with python shows how to run the windows shell command in python, so in the interactive python prompt, i could actually use
>>>os.system('python first.py')
to run first.py, but x and y defined inside are lost after running.

Try the following for Python 2.x:
>>> execfile('first.py')
For Python 3.x, try this:
>>> exec(open("./first.py").read())
The variables should then be available to you.

Use
C:\python>python -i first.py
to run the script and get the interactive shell in the same namespace afterwards.

Related

How do we create alias from python program/script? [duplicate]

This question already has an answer here:
create unix alias using a python3 script
(1 answer)
Closed 2 years ago.
I want to create an alias' through python program. I intend to use python as a replacement for shell scripting.
What I tried is:
import os
os.system('alias go2dir="cd /i/want/to/goto/this/dir"')
...and it does not work. I know the reason - that system command 'alias...' is getting executed in another shell and not in the current one where this python script is executed. So, that alias is not available to this shell.
What I don't know is - (In general,) how do we execute a command from a python program in the same shell where this python program is being executed. So that (in this case) the alias is available till the shell terminal is open?
The way other applications that want to automate actions in the user's shell work is that they write shell commands to their standard output. Then you can execute them with eval.
makealias.py:
print('alias go2dir="cd /i/want/to/goto/this/dir"')
Then in bash:
eval "$(python makealias.py)"
An example of a standard Unix program that works like this is tset with the -s option.

Run Python script on Terminal and keep using the terminal afterwards [duplicate]

This question already has answers here:
Run a shell script and immediately background it, however keep the ability to inspect its output
(3 answers)
Closed 4 years ago.
I have a script that I activate via the terminal that listens to events. I want to keep using the terminal after the script runs, but the script is running and I can't type anything. The only way I know that lets me keep typing is by using the Ctrl + C combination, but this stops the script.
So how can I run the script in the background and keep using the terminal without terminating it?
Edit: I tried to use the '&' operator but it didn't work:
You can run your script in the background with:
python myscript.py &
If your script does output something you can suppress the output with:
python myscript.py 1>/dev/null 2>&1 &
Or save the output for later:
python myscript.py 1>myoutputfile 2>&1 &
# ...
less myoutputfile
Another alternative would be to use for example screen or tmux.

Creating Environment Variables in Python does not seem to work [duplicate]

This question already has answers here:
Can a shell script set environment variables of the calling shell? [duplicate]
(20 answers)
Closed 5 years ago.
I am writing a python script and I want this script to write some value to an environment variable which does not exist before the running of this script. The OS which I am running is Ubuntu. So ideally, after execution of the script, I should be able to display the environment variable using the echo command on the terminal. By that I mean the terminal behaviour should be as such:
> echo $NEW_VAR
environmentvarvalue
I want to be able to access this environment variable in a different python script as well. So the following code:
import os
output = "The value of NEW_VAR is " + os.environ.get('NEW_VAR')
print output
when run after running the initial script, should produce the following output when executed:
The value of NEW_VAR is environmentvarvalue
I have tried the following code in my initial script after a bit of research about this:
import os
os.environ['NEW_VAR'] = 'environmentvarvalue'
but the environment variable gets saved only for the scope of the initial scrpt, and when I run the echo command on the terminal after execution of the script, the behaviour is the same as when the variable does not exist. Is there something wrong with my code? Or is there a better way of assigning an environment variable through python? I know that I can probably run the export command from the subprocess python module, but this does not seem like the right way to solve this problem.
You can't do this using environment variables. Your environment is destroyed when the script finishes. The parent process (your shell) will never see the variable.
You need a different approach. Possibly writing to a file or a database.

how to run python command line [duplicate]

This question already has answers here:
Running a python script from the command line in Windows
(3 answers)
Closed 6 years ago.
I have pycharm installed and do my class assignment in it and things work well. The teacher wants on to now run some of the programs in command prompt. Given that my programs and the path is here: "C:\Python27\python.exe C:/Users/sz5062/PycharmProjects/untitled1/main.py", how can I run a program that is saved the above path in command line? So, example, how would I run the main.py in command line, not shell prompt?
I have searched and searched for answers here, but I can't fine one. Thanks very much for your help.
This is the full instruction: Install Python and a programming text editor and write a program that prints one line other than 'hello world', then take two screen shots and upload them below. You should use the command line to execute the Python program you wrote in the text editor. Please do not use the IDLE Python Shell, the Python Interpreter (>>>), or a shortcut in your text editor to run the code.
When i run "python main.py", I get an error message "python is not recognized as inernal or external command".
On you Commandline enter:
C:\Python27\python.exe C:\Users\sz5062\PycharmProjects\untitled1\main.py
The above command should work from every directory.
First part is the program you start (python.exe). Second part is the argument you pass to the program(C:\Users\sz5062\PycharmProjects\untitled1\main.py). This is the path to your python code so that python.exe knows what you want to execute.
If you add C:\Python27\ to your PATH variable, you can navigate to your directory C:\Users\sz5062\PycharmProjects\untitled1\ and start your programm with:
python main.py
I'd recommend making sure that python is on your PATH (if you type python does it open a shell?) and then navigate to the C:/Users/sz5062/PycharmProjects/untitled1 directory. Once there you can do:
python main.py
and that should work.
I think I made this hard. since python was in installed on C:\python27, all I needed to run the command line was simply to write the code in a text editor, save it in the same directory and call it from there. So, essentially, c:\python27\code.py

Python end program in shell [duplicate]

This question already has answers here:
Interacting with program after execution
(3 answers)
Closed 7 years ago.
I want a script to run an then finish on the python shell with all variables and methods:
$ python myprogram.py
...
program output
...
>>>
And with #!/usr/bin/python is posible? so I double-click and it just works?
Sounds like you want Python's i flag. From the help menu:
-i : inspect interactively after running script; forces a prompt even
if stdin does not appear to be a terminal; also PYTHONINSPECT=x
So the full command would be
python -i yourscriptname.py

Categories

Resources