Spyder - UMD has deleted: module - python

I have been fooling around for about a month with python now and something is bothering me.
I use the python(x,y) toolkit, which comes with the neat Spyder IDE.
My question concerns the UMD (User module deleter) of Spyder.
I found this graphics module on the internet, which helps one to do some simple graphic stuff in a python script (as far as I understand).
It is not like i'm stuck, but when I execute the folowing code:
import pylab as p
import graphics as g
window = g.GraphWin("tryout", 600, 600)
window.close()
print p.sqrt(4)
The output is:
>>>runfile(r'C:\some\folders\tryout.py', wdir=r'C:\some\folders')
>>>UMD has deleted: graphics
>>>2.0
line 1 is obviously o.k. and so is line 3, but I don't get line 2.
Also, the provoked window flashes in and out of the screen, as it should.
Line 2 doesn't seem to do any harm, and i can perfectly rerun the file as many times as I wan't, but I want to know where it is comming from.
AFAIK UMD forces the interpreter to reload a module everytime a script is run.
Does the displayed message mean that 'it' has deleted the references to the module, because it isn't used anymore, or is it something else? Or does it mean something is wrong, and will it 'hurt' my code should I add more afterwards?
Note: first question, so please comment the crap out of it to help me improve my asking skills.
EDIT: i tried shifting around the test line print p.sqrt(4), and found out that it doesn't matter where I put it. If its the first line after importing the modules, it still raisses the message before showing sqrt(4)

Short Answer:
Perhaps deleted is not the best word in the message you mention. It should be reloaded, which is what UMD is really doing and because is way less confusing. I'll fill an issue for this in our issue tracker.
Long answer:
UMD reloads not only your script but also all the local modules it depends on. By local I mean modules outside your Python installation and over which you have writing permissions.
The idea is that next to your script, perhaps you have developed a library of auxiliary functions to go with it. So you most probably want to reload that library too, so that any changes to it are reflected at run time.
I know this is not your case, so if you want to remove that message, you can go to:
Tools > Preferences > Console > Advanced settings > User Module Deleter
and deactivate the option
Show reloaded modules list

Related

Trouble accessing function from imported module in the same directory

I just started learning Python two weeks ago. I know this question is probably so basic. But please explain to me like I'm 5. I googled "vscode python import not working" etc but all the other cases seem way more complicated than mine.
I'm trying to follow a tutorial where they're introducing me to IDEs and showing that, if the a .py file is in the same directory as your current file, you can import it by typing "import file_name". Except it's not working for me:
It doesn't say there's an error or anything when I just run "import file_name" but says the function in the imported file doesn't exist in the current file if I try to run that.
I did not click on "add blah to PATH" thing when I installed. Could that be the problem?
Some google results say to make sure the debugger uses the correct working directory. Does it pertain to my situation and if it does, how do I do that? Some other results said to add the directory to PYTHONPATH but I didn't understand what that does, also it sounded like that sets up a path for the current file but doesn't establish that that's where I want to pull a file from in general when I type "import file_name" in any file I make from now on.
Please help, both in understanding the problem and fixing it. Thanks.
What I've done so far:
I thought maybe the file name was a keyword so I changed it from area to my_area but it still didn't work. I don't have any other ideas as to how to fix this.
In simple terms, the triangle function is imported as part of the my_area module, and so you'll need to get it from there as my_area.triangle:
import my_area
print(my_area.triangle(4, 5))
Alternatively, you can directly import triangle:
from my_area import triangle
print(triangle(4, 5))
Here's the python module tutorial for more info.

PyCharm code completion works in console but not editor

When typing the following code into the editor window, only some of the available items for autocomplete show up. That is to say that it should show .loc as an option but doesn't.
import pandas as pd
df = pd.read_csv('somecsvfile.csv')
df.
code completion in editor window
When using the console in PyCharm with the same code, the full list shows up. (See the attached images)
code completion with the full list
I have invalidated caches and restarted. Further, it seems like another recommendation was to turn on Python Debugger -> Collect run-time types information for code insight. I did that as well and still nothing when in the editor window.
What really confuses me is that the code completion works in the console, but not the editor.
Any help would be greatly appreciated!
When you run it in the console it knows the type of df because it actually has it right there. It can even run dir(df) to know exactly what names are available. In the editor it isn't running the code so it has to guess the type by inspecting pd.read_csv which is much harder (often even impossible) because Python is so dynamic.
I used to have this same problem. This was only happening for me in Linux. Note that this is possible and actually standard behavior in windows, so it can be done. Not sure if it is done using static analysis or a similar method.
I have since been able to fix it, I think what did it was defining the correct interpreter not only in the running/debugging configuration, but also in the defaults of the project (check File-->settings-->Project Interpreter and File-->default settings-->Project Interpreter)
I have now moved to the next problem, which is that auto-complete works for Python Console and File editing, but weirdly enough does not work for Debugging!...

How to hot swap the current file in python

I am trying to hot swap a file in python. I am creating a game that takes a really long time to load. But I don't want to reload it every time. I am trying to change some code while the programme is in runtime.
For example:
I want to change this:
while True:
print("Hello")
to this while in runtime:
while True:
print("Hello World")
I looked hot swapping up for python and all of them are answers that I am not looking for. All the other answers change modules. I want to change the current file. Like java in eclipse. Please help!
I want to change the current file. Like java in eclipse.
When you modify Java code in Eclipse, the code is not currently running. In reality, Eclipse has a Java compiler built-in, which attempts to compile your code as you type it. That's how Eclipse is able to give you such fast feedback about syntax errors and type errors in your code. But it can't give you any information about the runtime behaviour of your code (whether or not it produces the right answer), because it doesn't run the code! You need to press the Run button for that.
So I think the question you really want to ask is not "Can I dynamically modify Python code?" (the answer to that is "Yes, but it's complicated and has caveats and is not a good idea") but "Does there exist a Python IDE which can give me feedback about syntax errors while I type?" The answer to that is an emphatic "Yes!". There's an extremely comprehensive list of options in this answer.
You can use dynamic execution using exec function.
Store the code you want to execute in a string and change it during runtime.

Python IDLE won't show docstring

My IDLE (Python 3.4.3) won't show functions doc-strings when typing the name of the function.
Is anybody familiar with this problem?
I've tried everything, including uninstall etc. Answers on the web are nowhere to be found.
I'm talking about showing the docstrings automatically, NOT when specifically typing :
print(func. __ doc __)
Thanks
Docstrings, are part of calltips, not completions. Calltips are shown when one types '(' after the name of an acccessible function. The calltip should stay displayed until one types ')' or clicks the mouse or otherwise moves the cursor to dismiss it. Cntl-\ brings it back.
A calltip consists of the function signature and the first line of the docstring. For builtins without an accessible signature (such as, in 3.4.3, int or bytes), the calltip consists of all lines up the fifth line or the first blank line.
The set of accessible functions depends on what modules have been imported into the user process (where your code is executed), including those imported by Idle itself, and what code has been run (since the last restart). For example, restart the Shell (Cntl-F6), open a new editor window, and enter
itertools.count(
A calltip appears because Idle imports itertools into the user process for its own use. Enter
turtle.write(
and nothing appears, because the Idle does not import turtle. Cntl-\ does nothing either. Entering
import turtle
above the function call does not immediately help, but if one runs the file to perform the import, calltips for turtle functions become available.
This suggests that one might want to run a file after writing the import statements at the top, or immediately run an existing file before editing.
Comments:
I suspect your problem is that you are trying to get a calltip for a function that is not currently accessible, even though it might have been accessible before and will become accessible after running your code.
I have opened issue 24028 to add something like the above to the Idle docs as a subsection on calltips after the subsection on completions
Existing issue 1350 is about adding the option to display the full docstring.
The availability issue is a nuisance. I have a couple of ideas for improving it. In the meanwhile, use the suggestion above about running your imports.
EDIT: 2018 Aug 2
Some combinations of Mac OSX or MacOS and tcl/tk require an addition of one line to idlelib/calltip_w.py (3.6+) or idlelib/CallTipWindow.py (3.5-). Issue 34275
self.label.pack() # Line 74
tw.update_idletasks() # ADD THIS LINE!
tw.lift()
This should be included in future releases. If the above does not work, please remove _idletasks

Navigating a big Python codebase faster

As programmers we read more than we write. I've started working at a company that uses a couple of "big" Python packages; packages or package-families that have a high KLOC. Case in point: Zope.
My problem is that I have trouble navigating this codebase fast/easily. My current strategy is
I start reading a module I need to change/understand
I hit an import which I need to know more of
I find out where the source code for that import is by placing a Python debug (pdb) statement after the imports and echoing the module, which tells me it's source file
I navigate to it, in shell or the Vim file explorer.
most of the time the module itself imports more modules and before I know it I've got 10KLOC "on my plate"
Alternatively:
I see a method/class I need to know more of
I do a search (ack-grep) for the definition of that method/class across the whole codebase (which can be a pain because the codebase is partly in ~/.buildout-eggs)
I find one or more pieces of code that define that method/class
I have to deduce which one of them is the one I need to read
This costs a lot of time, which is understandable for a big codebase. But I get the feeling that navigating a large and unknown Python codebase is a common enough problem.
So I'm looking for technical tools or strategic solutions for this problem.
...
I just can't imagine hardcore Python programmers using the strategies outlined above.
on Vim, I like NERDTree (a file browser) and taglist.vim (source code browser --> http://www.vim.org/scripts/script.php?script_id=273)
also in Vim, you can use CTRL-] to jump to a definition (:h CTRL-]):
download exuberant ctags http://ctags.sourceforge.net/
follow the install directions and put it somewhere on your PATH
from the 'root' directory of your source code, make a tags file from the shell: "ctags -R"
(make sure you have :set noautochdir, and make sure :pwd is the root directory from step 3)
go into Vim, cursor over some function or class name, hit CTRL-]
by default, if there's multiple matches for the tag, it shows you everywhere it was imported, and where it was declared
if the tag only has one match, it immediately jumps to it
...then use Ctrl+O and Ctrl+I to move back and forth from where you were
(repeat above steps for the source code of particular libraries you use, i usually keep a separate Vim window open to study stuff)
I use ipython's ?? command
You just need to figure out how to import the things you want to look for, then add ?? to the end of the module or class or function or method name to view their source code. And the command completion helps on figuring out long names as well.
Try red pill: https://github.com/klen/python-mode

Categories