I ran the commands attached below in my command line and it works, as it should, but not in JupyterLab. It seems odd but I was wondering what's going on?
The !cd datasets command did work. However, you aren't understanding what is going on with the use of the exclamation point. What the exclamation point does is open a separate temporary shell instance and does work returning what gets returned. The separate shell instance goes away. Poof
What you were trying to do was change the working directory within your notebook. And so you wanted to use:
%cd dataset
You'll want to learn about the many IPython/ Jupyter magic commands and use them. See here for the IPython ones that Jupyter inherits as Jupyter grew out of the IPython notebook project and so when using a Python-based kernel, you have those utilities. There are some others that are cell and line specific and specific to Jupyter. Here looks like good overview of this.
Note that generally in modern Jupyter, auto-magics is enabled by default. For all the shell commands you show in your post, you want to use the magic command equivalents. And so you are better off trying without any symbol first. Auto-magics will usually add the % behind the scenes. If that fails, then add the % if you are sure there is a magic equivalent that is the same as a shell command because maybe automagics aren't on by default on the system you are on. Finally, some of the similar tasks you'd perform in the shell have different syntax as a magic command in IPython/Jupyter. The example of the %store line magic comes to mind. It makes sense it is different though because the main thing it does is different than shell. However, there is some overlap with abilities the shell has. For example %store foo >a.txt that lets you send a value of a Python variable directly into a text file. Note that it doesn't allow a space after the redirect symbol! Unlike the typical shell redirect. Such a syntax difference can be puzzling when you first encounter it.
Related
I'm using Node for a lot of small JSON manipulation scripts, similarly to how one would use Python.
However, every time I want to change my output, I obviously have to edit my script file, save it, and run it using the node command.
Is there perhaps a way to directly write scripts in a "live" command prompt, similarly to a python shell / jupyter notebook?
Yes - there is.
You can use node REPL.
REPL stands for Read-Eval-Print-Loop and is the tool you are looking for.
See here for more info.
I need my Python program to do some stuff, and at a certain point give control to the user (like a normal Python shell when you run python3 or whatever) so that he can interact with it via command line. I was thinking of using pwntools's interactive() method but I' m not sure how I would use that for the local program instead of a remote.
How would I do that?
Any idea is accepted, if pwntools is not needed, even better.
Use IPython
If you haven't already, add the package IPython using pip, anaconda, etc.
Add to your code:
from IPython import embed
Then where you want a "breakpoint", add:
embed()
I find this mode, even while coding to be very efficient.
End goal: After running a script regardless of ending perfectly or erroring out, I would like to have my ipython environment revert back to the directory it was in before running the script.
I have successfully used the advice given in the SO post,
how-do-i-change-directory-back-to-my-original-working-directory-with-python.
This works great if the script runs without failing.
Ideas:
1) I have considered wrapping my code in one large try: and except: utilizing the advice in the post mentioned above. As this would surely handle the issue. However, it seems tedious and unnecessary to have to write this into all my scripts.
2) I was thinking a good a solution would be to have ipython automatically run a "revert directory" script after every user ran script, that would change the directory back to the original one. I have looked around a bit to see how one would configure ipython to run a script automatically after every run. From my search so far I have not found this to be an option. I was expecting to find a method to accomplish this through the ipython config file.
3) Another solution that would be easy would be to somehow use the information found by using the line magic command %dhist. As the first result given by this command is the original directory that ipython was opened in. However, other than having the results print from executing the command %dhist... I am not sure how to work with the printed information.
4) I must be not searching the right terms, as I feel like this is probably something that is possible or has been asked. If indeed someone has asked this, please point me in the right direction and I will remove this question promptly.
Relevant information:
Working on os x
Using Jupyter QtConsole 4.3.1
IPython 6.2.1
I just started using jupyter for a python project. I constantly find myself adding an extra cell just to perform some basic try&error debugging. This way I omit the whole code of the cell is being executed but it still doesn't feel like the right way to do it.
Does Jupyter provide something like a static kernel terminal, for example always visible at the bottom of the screen, where I can simply paste code and execute runtime variables?
By the way: I did search but didn't find anything looking for static console, and terminal. Maybe I'm just looking in the wrong direction.
Thanks!
The jupyter console command will provide you with an interpreter environment that you can experiment with code running within the Jupyter environment outside of a notebook.
It's not exactly what you're looking for but may provide a better environment for testing and developing code that you can then paste into the appropriate notebook.
EDIT: I got it working, I went into the pycassa directory and typed python pycassaShell but the 2nd part of my question (at the bottom there) is still valid: how do I run a script in pycassaShell?
I recently installed Cassandra and pycassa and followed the instruction from here.
They work fine, except I cant get pycassaShell to load. When I type pycassaShell at the command prompt, I get
'pycassaShell' is not recognized as an internal or external command,
operable program or batch file.
Do I need to set up a path for it?
Also, does anyone know if you can run ddl scripts using pycassaShell? It is for this reason that I want to try it out. At the moment, I'm doing all my ddl in the cassandra CLI, I'd like to be able to put it in a script to automate it.
You probably don't want to be running scripts with pycassaShell. It's designed more as an interactive environment to quickly try things out. For serious scripts, I recommend just writing a normal python script that imports pycassa and sets up the connection pool and column families itself; it should only be an extra 5 or so lines.
However, there is an (undocumented, I just noticed) optional -f or --file flag that you can use. It will essentially run execfile() on that script after startup completes, so you can use the SYSTEM_MANAGER and CF variables that are already set up in your script. This is intended primarily to be used as a prep script for your environment, similar to how you might use a .bashrc file (I don't know of a Windows equivalent).
Regarding DDL statements, I suggest you look at the SystemManager class.