Different responses from Spyder's Ipython and standard anaconda console - python

I have a very large script which I developed using Spyder. I have friends who are trying to run it using the standard anaconda console / command prompt. What is interesting is the the script works between computers if run on Spyder, but completely freaks out if run in the anaconda console, command prompt, or even Ipython outside of Spyder. When I say freak out, I mean it randomly starts again at the beginning of the script, reloads packages, spams the user interface text a few times then randomly crashs. There is never an error code. There is no while loop or for loop or anything which would cause it to restart randomly like this. I have had a number of python programmers look at the code directly and cannot understand either why it would even attempt to restart.
I have isolated the issue to the following line:
results = pd.DataFrame(MOR.predict(dat), columns = scores)
In this case, MOR is an imported pickled sklearn multioutput xgboost model. dat is a dataframe with a couple rows and the same required columns as the xgboost model. scores is simply a list of names which is equal to the number of output variables from the sklearn model.
Any ideas as to why this script would run perfectly in spyder but not the console? Note, its clear that this likely will not have a solved answer, but any speculations as to why spyder would behave different from these other consoles?

Related

Why is my python console running slow even for simple commands?

I have a script that takes about 90 seconds to run and it creates a dataframe that has 200,000 rows and 50 columns. (I've created larger datasets on this computer before without issue) When I run the initial script it runs at ideal speed. Then if I want to explore the data in the console after everything comes to a drag, even something simple like df.columns.
I stuck df.columns in the original script and it printed within a split second of it's turn in the script. But still when I then type it into the console afterwards I get the message "Backend MacOSX is interactive backend. Turning interactive mode on." Then it loads for 30 full seconds before spitting out the columns.
What is causing this slowdown?
Specs: python 3.8, conda env, Pycharm 2020.2

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

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

How to run code with Hydrogen

I am completely new to Atom.
I installed it and it felt quite easy to use and set up. I read that the Hydrogen package enables functionality similar to the Jupyter Notebook. So I installed the package.
Unfortunately, I have no Idea how to use it. I read the entire documentation (which isnt too extensive) and searched for everything I could.
So here is my problem:
I created a file called testfile1.py
In that file i put the very simple line
print(‘Hello’)
just to see how it works. I marked the line and pressed Ctrl+Enter. At the top right, a window pops up saying “Hydrogen Kernels updated: Python 3”.
But then nothing happens. I dont see the result of the code that I tried to run anywhere. I tried different lines of codes, tried differen run-combinations, nothing gives me any results.
I am using arch linux, installed Anaconda through the AUR to /opt/anaconda.
Using the terminal and running
jupyter notebook
for example works just fine and opens a Notebook in Firefox (as it should) and running code that imports modules that came along with Anaconda also work fine once i run them with the script package in Atom
(things like
import numpy as np
is letting me work with all the numpy funtions as expected. So I think that it shouldnt be any issues related to the Anaconda packages itself)
I tried to look everywhere I could, but I do not find any solution on why hydrogen would not give me any results.
Is there anything I am missing or did wrong?
I hope someone might be able to help me, thanks already in advance
I'm using Windows 10, Atom version 1.30.0 with Hydrogen 2.6.0 (both with default settings).
If you navigate to Packages > Hydrogen you can see the default key bindings.
To make a cell there are many options noted in the manual as shown below.
I found I had to re-start Atom after updating the python kernel. Then the run code commands worked as expected.
You need to select all the lines which you want to execute with the cursor and then press ctrl+enter (or shift+enter depending on your keybinding).
There are two ways to tell Hydrogen which code in your file to run.
Selected code:
If you have code selected when you hit Run, Hydrogen will run exactly that code.
Current block:
With no code selected, Hydrogen will try to find the complete block that's on or before the current line.
If the line you're on is already a complete expression (like s = "abracadabra"), Hydrogen will run just that line.
If the line you're on is the start of a block like a for loop, Hydrogen will run the whole block.
If the line you're on is blank, Hydrogen will run the first block above that line.

Compiler vs. IDLE Environment

I'm using mac osx mavericks (fully updated) and while looking for a pyscripter alternative, I decided to download CodeRunner from the app store.
I've noticed that certain programs I create in one application, don't always run the same in the other environment.
For example, there are times when I create a program using CodeRunner, but when I open the exact same program in the IDLE environment, it spits back an error. Usually complaining about the syntax or logic of the program. And the other way around can sometimes occur as well.
Is this normal behavior? Should I be saving the program in a different format? I assumed that since I'm coding in Python, the code would behave the same regardless of the environment I use.
The different programs probably have different definitions of tabs and whitespace.In IDLE, you can use Format->Tabify/Untabify menu options to change between tabs and spaces. When you click on these menu options, you prompted for the columns per tab, which will default to 4(At least on my machine). Hope this helps!

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.

Categories