Save Interactive Python (using script) - python

I would like to save the input and the output of a Python session when providing the input as a file.
In interactive mode, I would type a command of expressions and get their evaluation, as shown:
Python 2.7.11 (default, Mar 8 2016, 18:01:39)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> abs(3)
3
>>> abs(-3)
3
The text above is my desired output.
When I write these two expressions in p.py:
abs(3)
abs(-3)
and execute python p.py, I don't get any output, which is fine because Python runs in non-iteractive mode. If I execute python -i < p.py, I just get the output, not the input.
So, I dediced to use the script command.
However, with script, I get different results in MacOs and Linux (and none of them save what I would like).
If I execute script -q output.txt python -i <p.py on MacOs, file output.txtcontains
abs(-3)
abs(3)
Python 2.7.11 (default, Mar 8 2016, 18:01:39)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> abs(-3)
3
>>> abs(3)
3
which is the desired output but with the input of p.py preppended to it.
If I execute script -q output.txt -c "python -i <p.py" on Linux, file output.txt contains
Python 2.7.3 (default, Jun 22 2015, 19:33:41)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 3
>>> 3
which is far from I would like.
In brief:
How could I save a file that reproduces the interactive Python session when redirecting input to it?
Additionally,
Why do the MacOs and Linux implementations of script work so differently?

Related

When I Run my .py files, It gave me an invalid syntax error

I use the python 2.7.12. My .py file contains the following:
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print "hello"
When I try to run the file I get the following error:
File "/private/tmp/fun.py", line 1
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
^
SyntaxError: invalid syntax
I just use the print "hello", so I don't think it's a code problem
Who can tell me what's wrong?
Put in file only
print "hello"
without
Python 2.7.12 ...
>>>
because Python ... >>> is not Python code but text displayed by Python Shell only for your information

Caffe import working with python2 but not python2.7 / ipython notebook, despite both being 2.7.10?

I've successfully compiled the caffe library and the python module.
I can do this:
Jamess-Air:~ james$ python2 -V
Python 2.7.10
Jamess-Air:~ james$ python2
Python 2.7.10 (default, Jul 13 2015, 12:05:58)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import caffe
>>>
But, bizarrely, this fails:
Jamess-Air:~ james$ python2.7 -V
Python 2.7.10
Jamess-Air:~ james$ python2.7
Python 2.7.10 (default, Jun 19 2015, 15:39:31)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import caffe
Segmentation fault: 11
Jamess-Air:~ james$
I cannot understand this at all! Whenever I try to run using iPython notebook I get the same crash.
Any ideas as to what may be causing this, and how I might fix it, or at least get iPython Notebook to use the different python version so I can run this thing?
Thanks!

Control characters in Python shell

I have a problem when I am using Python interactive shell.
I love emacs-style key bindings, but when I typed <ctrl> A or <ctrl> K, it echo ^A or ^K. Like:
:)[12:16]root:~ # python
Python 2.7.9 (default, May 15 2015, 01:13:44)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print ^A^K^H
Is it due to readline lib? I have updated readline, but it seems no help.
Install the readline development package and rebuild and reinstall Python.

libsvm on linux - how to add path

I downloaded the tar file for libsvm, navigated to the python directory and ran the make command. This words and when I run python inside that same directory,
import svm
works just fine. But not in any other directory. What can I do to make this library accessible from any where? I know it has some thing to do with copying the path some where, but not sure since I'm a newbie at linux.
what linux distro are you on? on my Ubunto I just:
$ sudo apt-get install python-libsvm
...
$ python
Python 2.7.3 (default, Sep 26 2013, 20:03:06)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import svm
>>>
$ cd /tmp
$ python
Python 2.7.3 (default, Sep 26 2013, 20:03:06)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import svm
>>>

Adding a shebang causes No such file or directory error when running my python script

I'm trying to run a python script. It works fine when I run it:
python2.5 myscript.py inpt0
The problem starts when I add a shebang:
#!/usr/bin/env python2.5
Result in:
$ myscript.py inpt0
: No such file or directory
Try 2:
#!/usr/local/bin/python2.5
Result in:
$ myscript.py inpt0
: bad interpreter: No such file or directoryon2.5
When I run them directly in the terminal they both work just fine:
$ /usr/local/bin/python2.5
Python 2.5.4 (r254:67916, Feb 9 2009, 12:50:32)
[GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-52)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
$ /usr/bin/env python2.5
Python 2.5.4 (r254:67916, Feb 9 2009, 12:50:32)
[GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-52)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
Any hints on how to make this work with shebang?
I had similar problems and it turned out to be problem with line-endings. You use windows/linux/mac line endings?
Edit: forgot the script name, but as OP says, it's dos2unix <filename>

Categories