Pydev Django project compilation - python

I installed pydev and eclipse to compile a django project. Everything looks good but one thing makes me crazy. When i change something in the code, i click to save it and hope to see the effect of changes. However, I cannot see the effect of what i change unless I terminate the program and re-run as as shown below. It is such a pain...
I used to use Pycharm, but it expired. In Pycharm, when the program runs once, i do not need to run it again and again. I can easily see the effect of my changes on the code by clicking save button. Is it possible to see the same thing in pydev and eclipse? Do you guys also see this issue?

To debug Django with the autoreload feature, the Remote Debugger must be used and a patch must be applied to your code (just before the if _name_ == "_main_": in your manage.py module):
import pydevd
pydevd.patch_django_autoreload(
patch_remote_debugger=True, #Connect to the remote debugger.
patch_show_console=True
)
So, doing that, starting the remote debugger and making a regular run should enable all the regular breakpoints that are put inside Eclipse to work in the Django process with the Remote Debugger (and the --noreload that PyDev added automatically to the launch configuration must be removed).
I have plans on improving that so that the debugger will automatically add tracing to spawned processes (probably for the next release), but on PyDev 3.3.3 this still requires doing this manual patch and using the remote debugger.
The above is related to a debug run. Now, on to the regular run...
When you do a run as > pydev: django, it should create a run configuration (which you can access at run > run configuration). Then, open that run configuration > arguments and remove the '--noreload' (leaving only the 'runserver' argument).
Then, you can simply rerun it with Ctrl+F11 (if you've set it to launch the previously launched application as indicated in http://pydev.org/manual_101_run.html)
-- (Or alternatively you can run it with: Alt + R, T, 1).
The only problem is that if you kill the process inside Eclipse it's possible that it leaves zombie processes around, so, you can use the pydevd.patch_django_autoreload(patch_show_console=True) as indicated above to open a console each time it spawns a new process (you can do a pydevd|<- ctrl space here to add an import to pydevd).
Note that this should work. If it's not working, make sure you don't have zombie processes around from a previous session (in windows you can do: taskkill /F /IM python.exe to make sure all Python processes are killed).
Another note (for when you don't actually have automatic reload): Ctrl+Shift+F9 will kill the currently running process and re-run it.

Related

Django start an old project [duplicate]

I just started using python and pycharm. I'm a bit confused of what is the run configuration in pycharm do and what is the different between the just run ?
A run configuration (not just in PyCharm, for example JetBrains IntelliJ also has them, in fact most IDEs have this concept) is a compilaton of settings to be used when running a program.
Let us stay with Python for the sake of simplicity. You might think that when you execute your script by typing in your command prompt...
python myscript.py
...that there are no settings or configuration involved. You are just running your script, right?
Not quite, you are in fact using what you could call an implicit run configuration, i.e. are whatever defaults and environment settings happen to take effect.
Some examples you will also find in PyCharm Python run configurations:
Script path is just the script you are calling, in the example myscript.py since we specified that on the command line.
Python interpreter is whatever Python interpreter is first in your path.
Parameters is empty in our example, as we didn't specify any on the command line.
Working directory is the current directory, where we are with our command prompt.
Enviromnent variables are those that happen to be set in our shell.
All of these and more can be defined in a run configuration (or multiple different run configurations if you need) for your project.
You can then select these conveniently from the dropdown menu, and the one currently selected will be used to execute your program when you press the green play button.
What is the difference between using a run configuration and just run in PyCharm?
If you just run your program, you are telling PyCharm that it should just use the project default configuration for the specific file type.
In other words, you are using a run configuration as well there, just the unmodified default configuration.

How to launch PyCharm's debugger with breakpoint()

I understand that I can set which debugger is launched by setting sys.breakpointhook(), but what do I set it to in order to launch PyCharm's IDE?
To clarify, I want PyCharm's debugger to launch when it encounters the breakpoint() builtin, especially if I'm running the program from PyCharm.
If you're wondering "why ever do this when you could just run from the debugger?" I'm trying to debug some code that responds differently when started with the debugger.
PyCharm uses the pydevd debugger https://pypi.org/project/pydevd-pycharm/. So assuming you want to debug a Python application started outside of PyCharm, you have two options:
either attach the process, see https://www.jetbrains.com/help/pycharm/attaching-to-local-process.html#attach-to-local or
start Pycharm as debug server and than connect to it from your application, see https://www.jetbrains.com/help/pycharm/remote-debugging-with-product.html.
The second option is probably closer to what you have in mind, i.e. instead of setting the debugger using sys.breakpointhook, you install the debugger using pydevd_pycharm.settrace(...) which then connects to PyCharm (where you can set the breakpoints in the source files).
Installing the custom breakpointhook by pointing sys.breakpointhook to the pydevd breakpointhook (i.e. pointing to the "Pycharm Debugger") will be done by pydevd (https://github.com/fabioz/PyDev.Debugger/blob/37d804c7ac968694ce29c93392e3bed6fda641f0/pydevd.py#L95, line 117 to be precise).
Ueli's answer got me 99% of the way there. Here's the summary.
If you want the PyCharm debugger to start, all you need to do is import it. There are a couple ways:
You can use the pydevd-pycharm.egg from the PyCharm installation (<PyCharm directory>/debug-egg/pydevd-pycharm.egg) or install the pydevd-pycharm package using pip.
If you're running things locally, the simplest would just be to add
sys.path.append("<PyCharm directory>/debug-egg/pydevd-pycharm.egg")
import pydevd_pycharm
somewhere in the program you are debugging.
If you're Trying to debug on a remote machine, you will need to install pydevd-pycharm:
pip install pydevd-pycharm~=<version of PyCharm on the local machine>
before adding
import pydevd_pycharm
Somewhere in the program.
The instructions for how to import pydevd_pycharm is coppied from step 4 of https://www.jetbrains.com/help/pycharm/remote-debugging-with-product.html#remote-debug-config (as linked in Ueli's answer).

PyCharm running Python file always opens a new console

I initially started learning Python in Spyder, but decided to switch to PyCharm recently, hence I'm learning PyCharm with a Spyder-like mentality.
I'm interested in running a file in the Python console, but every time I rerun this file, it will run under a newly opened Python console. This can become annoying after a while, as there will be multiple Python consoles open which basically all do the same thing but with slight variations.
I would prefer to just have one single Python console and run an entire file within that single console. Would anybody know how to change this? Perhaps the mindset I'm using isn't very PyCharmic?
There is a specific option in PyCharm 2018.2+: Settings | Build, Execution, Deployment | Console | Use existing console for "Run with Python console".
Run with Python console is an option you have enabled in the Run Configuration. Disable it if you don't need a Python console after a script execution:
Hi: If you are looking for re running the code again in the same python console everytime then you have to check the respective box in the Project settings as shown in image below.
To allow only one instance to run, go to "Run" in the top bar, then "Edit Configurations...". Finally, check "Single instance only" at the right side. This will run only one instance and restart every time you run.
One console is one instance of Python being run on your system. If you want to run different variations of code within the same Python kernel, you can highlight the code you want to run and then choose the run option (Alt+Shift+F10 default).
You have an option to Rerun the program.
Simply open and navigate to currently running app with:
Alt+4 (Windows)
⌘+4 (Mac)
And then rerun it with:
Ctrl+R (Windows)
⌘+R (Mac)
Another option:
Show actions popup:
Ctrl+Shift+A (Windows)
⇧+⌘+A (Mac)
And type Rerun ..., IDE then hint you with desired action, and call it.
I think that what you are looking for is the last option in this window; check it and it should work.
Settings -> Build, Execution, Deployment -> Console

Run a module in IDLE (Python 3.4) without Restart

It appears that, in the past, IDLE did not restart (clean the environment) when you ran a script (module). Today, however, this is the case. But for prototyping I would like the environment (assigned variables, imported modules, functions, ...) to survive running different modules (files).
Example: I am working on a function, let's call it f7(), that requires a certain environment. The environment is built in another script (file), say, env1.py. After env1.py has been run, I can built on all imported modules, defined functions and assigned variables, when working at the command line of IDLE. But I cannot run another file, where my f7() resides! I would have to redefine f7() at the interpreter's command line. Which I of course do not do, because f7() is very lengthy. The only thing that remains is to include f7() in env1.py. And restart it after every change to f7(). As a consequence, I have to wait each time until env1.py has finished. Which is a waste of time, because every time it runs, it does the same. I only change f7()...
Can I tell IDLE not to restart (clean environment) each time I run a module (file) in IDLE? If not, what alternatives to IDLE are capable of something like this??
It seems IDLE behaves the same on Windows, Ubuntu, Raspbian. I am using Python 3.X on each of these systems.
I am not aware that IDLE ever didn't restart when running a editor file, so that would have to have been several years ago. I will think about it as a new feature though.
EDIT: Added in June 2019: On the editor Run menu, Run... Customized opens a dialog with [X] Restart. Uncheck that box and the restart is skipped.
END EDIT
In the meanwhile, you can do this for the specific scenario you gave. Load env1.py into an editor window and run it. When >>> appears, enter or paste the def statement for f7 and run it. (Paste after loading the file with f7 and copy.) Test by calling f7. To edit the definition of f7, recall it to the current >>> line. Either click on the previous definition and hit Enter or use the history keyboard shortcuts (for me on Windows, Alt-P for Previous, Alt-N for Next). In either case, edit and re-run. Do the same with test statements. I recall and edit statements routinely.

How can I debug Python code without running a script (using Eclipse)?

I've noticed how easy it is to debug a Python script from Eclipse. Simply set breakpoints and run a Python script from the debug menu. But is it possible to start a Python Interactive Interpreter instead of running a particular Python script, whilst still having Eclipse breaking on breakpoints? This would make it so much easier to test functions.
Thanks for any help
Still looking for a simple/ish way to start the debugger in Eclipse->PyDev that lets me use the interactive debugger. None of the answers as of yet is acceptable
You can explicitly write code to setup a breakpoint in your script, and then "remote debug". That means having pydevd in the pythonpath of your script wherever it is running, and running the eclipse pydev remote debugger on your devbox. If it's all happening on the same machine, this is fairly straightforward.
If not, you'll need to specify the hostname of the dev machine running the python remote debugger in the call to settrace(). You'll also need pydevd available on the machine running the script.
I have got this working in the past without having to install eclipse+pydevd on the machine running the script. It's not totally straightforward, and if you go that route I'd recommend checking that the pydevd versions match or at least you know they're compatible. Otherwise you end up wasting time debugging the debugger.
For details see: Pydev Remote Debugger
what about this, in the script, you can write a function, say onlyForTest, then each time you write a new function and want to test it, you can just put it in the onlyForTest function, then specify some arguments required by the new function you just write, then open the interactive python shell, import the script, call the onlyForTest function, check out the result.

Categories