Create console in python - python

I'm looking to have the same functionality (history, ...) as when you simply type python in your terminal. The script I have goes through a bunch of setup code, and when ready, the user should have a command prompt. What would be the best way to achieve this?

Either use readline and code the shell behaviour yourself, or simply prepare the environment and drop into IPython.

Run the script from the console with python -i. It will go through the commands and drop you in the usual Python console when it's done.

Related

iPython automatically at launch in Visual Studio Code on Os X

I would like that iPython run automatically when I launch VSC instead of typing ipython and press enter in the terminal. The answer here How to set ipython/jupyter as the default python terminal for vscode? doesn't work as it is for windows but it shouldn't be really different. Also, is there something similar to the 'Execute' button in Spyder instead of typing %run filename ? Thanks !
I presume you mean you want to run the "Python Interactive Window" and not just an iPython console on startup
There is currently no way to run it on startup. At least no way without writing another extension that would run a command when opening a workspace. It would be simple for us to add one though. Probably a workspace setting. Can you log an issue here:
https://github.com/Microsoft/vscode-python/issues/new
For you second question, 'Execute' in spyder, we have 'Run Current File in Python Interactive Window'. This works on any python file. You can get to it through the context menu on a file or through the command palette.
Sadly the nice workflow of spyder is not provided by any official extension at the moment (as far as I know).
But you can implement the basics easily on your own by writing an extension. Even with no experience in TypeScript you can quickly build an extension which starts an IPython console as soon as you open a python file. I also managed to execute a startup script which implements the runfile method. VS Code also allows keybindings for your functions, so that you can almost work like you can with spyder.
Spyder modified the IPython terminal quite a bit though, so it won't feel exactly the same. But after all, everything there is open source so you could implement it yourself, which is what I'm trying to do in my free time.

python-shell history

When I code in shell for practicing and when I am done I close it, when I reopen it I cannot code in it again I have to open a new one !
How can I code in the same old one of shell ??
python command doesn't preserve your work
You can try installing IPython, Jupyter, or use a proper IDE for practicing
The python shell isn't meant to write full programs. It's nice for testing small pieces of code, but if you'd like to continue previous code, use the Python IDLE that comes with the standard download installation (in python shell -> File -> New File). This will require you to save the file to run the code. However, this IDE is not very user friendly. As user cricket_007 has mentioned, there are other IDE's that have autocomplete and other helpful tools.
Start using a terminal multiplexer like tmux or screen and launch python under it. My favourite is tmux. Your python session would persist till you kill the tmux session or till logout / reboot

Saving entered command in windows command prompt

Dear fellow developers,
I'm repeatedly using (and developing) a python script for calculations, by executing it through the windows command prompt in each test.
The script has some parsed options.
In order to make each of my calculations easily reproducible, I save the actual command I entered to execute each calculation. For the moment I simply copy by hand the command once I executed it and I put it in a file. But since I have to do it for each calculation, I wonder is there is any python script line that could take my command line input, like:
python script.py --option="foo"
into a file.
The form of the command could be:
%save file=_command_used.txt% python script.py --option=foo
which would create the file and save the actual command "python script.py --option=foo" into it.
Thanks in advance!
Best regards!
I would love to have solutions for both Windows command prompt and Linux shell command prompt.
On Linux there is the script command that will capture all entered commands in a file. Use it like that:
script -a _command_used.txt
python script.py --option=foo
python script.py --option=bar
The -a option stands for append so the _command_used.txt will not be overwritten.
On Windows you can achieve a similar thing using Start-Transcript and Stop-Transcript cmdlet. See this related post.
Since you are using Python, I recommend you investigate the Xonsh shell as one way to solve this. It is cross platform and is scripted with python.

How did I get informations on the module I'm writing?

I'm writing my first complete python project with Vim. As I was modifying a
file I accidentally hit several keys that I can't find back and I get this
prompt:
I didn't know it was possible to get this kind of help on a module I am
writing and I have no idea how I got it, so my question is:
What command or tools allows to generate this kind on module information?
Several notes
The command is not a Vim command because the ouput was in an external
shell (so I probably use an equivalent to :![command].
I don't have any Vim plugin related to python installed so it was probably not generated by a plugin.
The command wasn't issued in an interactive python prompt since I started my vim from my bash prompt.
I have not idea of how many keystrokes I used.
My Vim command history and my bash history doesn't have a trace of what
happened.
I'm using zsh and oh-my-zshell
I know that this question might sound silly but I have no idea of which tool can do that and I have no mean to find what sequence of keystrokes I used.
You can use pydoc command to get module help
pydoc requests
if you are using the interactive python shell, you can use the help function:
>>> import requests
>>> help(requests.get)
it work on class instance too

Automatically execute commands on launching python shell

I was wondering if there is a way to automatically run commands on entering the python shell as you would with the .bash_profile or .profile scripts with bash. I would like to automatically import some modules so I don't have to type the whole shebang everytime I hop into the shell.
Thanks,
Yup you can use the PYTHONSTARTUP environment variable to do this as outlined here
Also consider using ipython if you're doing a lot of interactive work. Your options for this kind of automation expand significantly.

Categories

Resources