I was trying to modify pdb++ and add watch capability to it. However, I found that it was already implemented, that is, display in pdb++ does watch the variables.
I'm able to watch all the variables inside my test file (test.py) if I do
python pdb.py test.py
But, if I add the following code inside test.py (and ran it as python test.py), I'm not able to do many functions like display, sticky, etc.
import pdb as bp
bp.set_trace()
Does anyone have any ideas what I'm missing? Thanks!
Related
In Blender V2.81a with Python there is a main script that work fine. The main script says
import f1, f2, ...
which does also work. Those scripts f1,f2,... are edited in an extern editor (in this case Notepad++). After saving they should reload when running the main script in Blender again. However (due to the way Blender works?) the 'old version' of the scripts are still active and a new (i.e. after editing) define is 'unknown'. In other words: the scripts doesn't seem to reload at all.
How do I import f1,f2,... in such a way that their new content is passed to Blender?
(this question is answered partially elsewhere but either those solutions are not working in Blender V2.81a or the solution works only in the special case mentioned in those answers; is there a generic way to reload all scripts, always and in all cases? I also couldn't find a Python statement to reset the whole Blender session, which could be an alternative option?)
I have a source code consists of many custom modules.
In the first few lines, there are import words such as...
import custom_module_1
import custom_module_2
import custom_module_3
....
When I run(shortcut is F9) this code with partial or full selection, I found ModuleNotFoundError: custom_module_1.
However, when I run(F5) code, I works well runfile('C:/Users/user/Desktop/test.py', wdir='C:/Users/user/Desktop')
I am so confused because I thought 'run(F5)' and 'full selection run(F9)' are same.
However the results are very different.
Is there any different between 'run(F5)' and 'full selection run(F9)' in Spyder?
(Spyder maintainer here) The difference is the following:
Run selection takes the code you've selected in the Editor, pastes it in the console and runs it.
Run file is similar to executing python myfile.py, but before doing that it changes the directory your code will be run to the one your file is placed in. It'll also run it in a clean namespace so it's not affected by the variables currently defined in the console. Especially due to this last feature, you should avoid using Run selection as much as possible.
In your case I think the problem is that Run selection doesn't change directories, so Python can't find the modules you have next to test.py.
As you know, there is Main.py and we can make a multiple files
even yesterday, I made a new file(not Main.py) named so.py and it works well when I press run button
but today I try to run like that there is nothing on the result screen
Well if it was working yesterday and not today, then it might be a connection issue when you were trying to run the code this time around. That sometimes happens to me as well, but a refresh or two should work.
You've probably done that already so try copying your code to a new repl and try again.
Check for missing code you might have deleted, like an import statement in main.py which would explain why code isn't showing up. Assuming you're calling code from a different file or package.
Kindly note that your question isn't very clear so more details pertaining to your question would be appreciated.
Okay, one thing to clarify is that repl always runs main.py so you'll have to enter the following to have access to functions from the other files.
import indeed
import so
If you're expecting to run so.py by itself on repl I don't think it's going to work.
Now that you have them imported you can use any function in so.py by using the . notation.
e.g:
so.print_hello()
Or you could also import a specific function into main.py using this method
from so import function_name, function2_name, etc...
To make it as clear as possible here is example code if I had done it.
In so.py
def print_hello():
print('Hello, World!')
In main.py
import so
so.print_hello()
Output will be
Hello, World!
I'm sure someone has come across this before, but it was hard thinking of how to search for it.
Suppose I have a file generate_data.py and another plot_utils.py which contains a function for plotting this data.
Of note, generate_data.py takes a long time to run and I would like to only have to run it once. However, I haven't finished working out the kinks in plot_utils.py, so I have to run this a bunch of times.
It seems in spyder that when I run generate_data (be it in the current console or in a new dedicated python interpreter) that it doesn't allow me to modify plot_utils.py and call "from plot_utils import plotter" in the command line. -- I mean it doesn't have an error, but it's clear the changes haven't been made.
I guess I kind of want cell mode between different .py files.
EDIT: After being forced to formulate exactly what I want, I think I got around this by putting "from plot_utils import plotter" \n "plotter(foo)" inside a cell in generate_data.py. I am now wondering if there is a more elegant solution.
SECOND EDIT: actually the method mentioned above in the edit does not work as I said it did. Still looking for a method.
You need to reload it:
# Python 2.7
plotter = reload(plotter)
or
# Python 3.x
from imp import reload
plotter = reload(plotter)
Is there any way to create a virtual import path in Python?
My directory structure is like this:
/
native
scripts
some.py
another.py
[Other unrelated dirs]
The root is the directory from where the program is executed. Atm I add native/scripts/ to the search path so I can do import some, another instead of from native.scripts import some, another, but I'd like to be able to do it like this:
from native import some
import native.another
Is there any way to achieve this?
Related questions:
Making a virtual package available via sys.modules
Why not move some.py and another.py out into the native directory so that everything Just Works and so that people returning to the source code later won't be confused about why things are and aren't importable? :)
Update:
Thanks for your comments; they have usefully clarified the problem! In your case, I generally put functions and classes that I might want to import inside, say, native.some where I can easily get to them. But then I get the script code, and only the script code — only the thin shim that interprets arguments and starts everything running by passing those to a main() or go() function as parameters — and put that inside of a scripts directory. That keeps external-interface code cleanly separate from code that you might want to import, and means you don't have to try to fool Python into having modules several places at once.
In /native/__init__.py, include:
from scripts import some, another