How to configure Ipython to automatically revert back to the directory it was opened in after running a script, even if the script errors out - python

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

Related

Change directory in Jupyter Lab not working

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.

How can I access functions inside the anaconda3/bin directory when running a bash script with subprocess.call?

I have the following problem: I wrote a bash script for data analysis that works perfectly fine when I run it from the terminal. To further automate the process I wanted to use a python script that runs the bash script (using subprocess.call), changes the working directory, and reruns the script (and so on). This also worked fine when I did it on my MacBook. However, I need to do the analysis on a Linux machine and here the problem occurred. Again, running the script from the terminal worked fine but once I tried doing this with my python script it fails to find the relevant functions for the analysis. The functions are stored inside the anaconda3/bin folder.
(Python does not even find other functions like "pip")
Of course, I could add the path to all the functions in the bash script but this seems very inefficient to me. So my question is: is there any better way of telling python where to look for the functions? And can you maybe explain to me why running the script from the terminal works but not when I use subprocess.call?
Here is the python script:
import subprocess
import os
path_list = ["Path1",
"Path2"
]
for path in path_list:
os.chdir(path)
subprocess.call("Users/.../bash_script", shell=True)
I'm just posting my series of comments as an answer since I think this at least constitutes a reasonable answer for anyone running into a similar issue (your question could definitely be common enough to index from search engine results).
Issue:
...running the script from the terminal worked fine but once I tried doing this with my python script it fails to find the relevant functions for the analysis
In general, you can troubleshoot this kind of problem with:
import subprocess
subprocess.call('echo $PATH', shell=True)
If the directory that contains the relevant binaries/scripts/etc. is not in the output, then you are facing a PATH issue in the shell created by subprocess.call.
The exact problem as confirmed by the OP in comments is that anaconda3/bin is not part of your PATH. Your script works in a regular terminal session because of the Anaconda initialization function that gets added to your .bashrc when installing.
Part of an answer that is very helpful here: Python - Activate conda env through shell script
The problem with your script, though, lies in the fact that the .bashrc is not sourced by the subshell that runs shell scripts (see this answer for more info). This means that even though your non-login interactive shell sees the conda commands, your non-interactive script subshells won't - no matter how many times you call conda init.
Solution 1: Manually use the Anaconda sourcing function in your script
As the OP mentioned in the comments, their workaround was to use the initialization function added to their .bashrc in the script they are trying to run. Although this perhaps feels like not a great solution, this is a "good enough" workaround. Unfortunately I don't use Anaconda on Linux so I don't have an exact snippet of what this looks like. See the next section for a possibly "cleaner" solution.
Solution 2: Use bash -i to run your script
As mentioned in the same answer linked above, you might be able to use:
bash -i Users/.../bash_script
This will tell bash to run in interactive mode, which then properly sources your .bashrc file when creating the shell. As a result, Anaconda and related functions should work properly.
Solution 3: Manually add anaconda3/bin to PATH
You can check out this answer to decide if this is something you want to do. Keep in mind they are speaking about a Windows OS but most of the same applies to Linux.
When you add the directory to your PATH, you are specifically telling your system to always look in that directory for commands when executing by name, e.g. ping or which. This can have unexpected behavior if you have conflicts (e.g. a command is found with the same name in /usr/bin and .../anaconda3/bin), and as such Anaconda does not add its bin folder to your PATH by default.
This is not necessarily "dangerous" per se, it's just not an ideal solution for most people. However, you are the boss of your own system. If you decide this works for your particular workflow, you can just add the export to your script:
export PATH="path/to/anaconda3/bin:$PATH"
This will set the PATH for use in the current shell and sub-processes.
Solution 4: Manually source the conda script (possibly outdated)
As mentioned in this answer, you can also opt to manually source the conda.sh script (keep in mind your conda.sh might be in another directory):
source /opt/anaconda/etc/profile.d/conda.sh
This will essentially run that shell script and add the included functionality to the current shell (e.g the one spawned by subprocess.call).
Keep in mind this answer is quite a bit older (~2013) and may not apply anymore, depending how much conda has changed over the years.
Notes
As I mentioned in the comments, you may want to post some related questions on https://unix.stackexchange.com/. You have an interesting configuration challenge that may be better suited for answers specifically pertaining to Linux, since your issue is sourcing directly from Linux shell behavior.

Issue with Python Batch file to run Python through Notepad++

EDIT: The code I wrote in my Python file was just this:
print "foo"
I'm using Windows XP Home Premium on this tiny little HP Mini 1000, and I want to run Python files, since we're learning it in school. I am aware of this topic, so I tried to run Python files using a batch file (python.bat), and I'm getting an error that says, "Can't find 'main' module in ''" whenever I run the batch file. I followed the instructions given here. All I did was change "Python26" to "Python33" because of the difference in versions.
Any idea what's wrong here? I really want to run Python files from Notepad++, so I don't want any alternative ways to run them.
This sounds like you don't have PYTHONPATH set up correctly. I suggest you review the documentation here:
http://docs.python.org/2/using/windows.html
Instead of calling Python, call cmd.exe and then use the set command to inspect which variables are set and how they are set. Run the exit command to leave the command shell. When you think you have the variables set up correctly, try again to run Python.
Good luck and have fun!
I use the command line interpreter or IDLE mostly (Win 8.1 now, but I've done so since Win XP SP2), but NPP is my main text editor, so I was curious about this issue.
When I was reproducing this, I was able to generate several errors, but the only one I got that was an exact match was when I failed to configure the Run option correctly.
You need to make sure to follow this step exactly in the instructions you were following. When you navigate to Run -> Run in Notepad++, you have to enter this exactly:
C:\Python33\python.bat "$(FULL_CURRENT_PATH)"
I am pretty sure you left out the "$(FULL_CURRENT_PATH)", or otherwise didn't add it correctly, as failing to do so causes exactly the same error on my end. Failing to include this means that when you run the batch script, you get the wrong input to the Python interpreter, causing the error.

running quickly application using pycharm

I was wondering how I could run a quickly application using pycharm. The way which I would hope to do it is instead of saying:
$ quickly run
In the terminal, I would Hope that I could instead just say something like
$ python bin/myapp.py
(That is the suggestion here, but it doesn't seem to work for me). That would mean I wouldn't have to do any weird setup on the python interpreters. However, if I need to, I can adjust pycharm's settings.
Ok, I found the answer when looking to add an interpreter. go to run > edit configuration.
I don't know if this is necessary, but I just set the working directory for the project interpreter to be the application one (it might do this by default).
Down below that, there is a place with a + icon above it. Click there to add an external tool.
The necessary settings for that tool is program: "quickly" and parameters: "run" (both without quotes).
Again, I'm not sure if it's nessecary to set the working directory here, but I did it just to be safe.

How to get pycassaShell working in windows?

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.

Categories