How to run adev(aiohttp_devtools) command using PyCharm - python

I want to use PyCharm debugger with aiohttp_devtools, but have no idea how to run command:
adev runserver --no-livereload
in this window

Add a new file to run the CLI and reference that in the debug setup:
adev.py:
from aiohttp_devtools.cli import cli
if __name__ == '__main__':
cli()
Then reference that to setup debugging:
With that everything worked fine for me.
Context:
I've fixed the issue above referenced by Andrew #99 and will create a new release v0.5.0, however that doesn't actually fix the problem here. I'll explain how to get debug working below.
I do use pycharm, but I steer clear of debug and similar features: partly because (as in this case) they're generally not worth the effort, and partly so when someone comes up with a decent opensource or paid IDE for python I can get away from pycharm's awful bugginess and awful customer service without much hassle.
The thing to remember when wrestling with pycharm is it was built by java developers in a hurry, not python developers; so it often deviates significantly from the pythonic way of doing things.
For example, in this case the developer clearly hadn't heard about python -m "run module as a script" or the virtualenv env/bin extension to $PATH.

Thank you for raising the question.
Right now there is no easy way to do it.
The only solution is creating own custom startup script which instantiates dev server like from aiohttp_devtools import cli; cli().
But I've created an issue for your needs: https://github.com/aio-libs/aiohttp-devtools/issues/99

I've managed to setup debug configuration with 'Module name' instead of 'Script path' and external adev.py script:
Also, you may need to set proper 'Working directory' and PYTHONPATH (mark directories as sources roots for that).

Related

What Does PyCharm Do To Make It Easy To Import Other Modules?

I have a question regarding simple imports that I cannot get my head around.
Take a look at the attached screenshot to see my project layout.
The file somefile.py imports the class SayHello from a file called someclass.py and calls it. someotherfile.py does the exact same thing. They both use from someclass import SayHello.
In Pycharm both files run. However, From the command line or from VSCode somefile.py runs, but someotherfile.py errors out with the following error:
ModuleNotFound: No module named 'someclass'.
I believe it has something to do with PYTHONPATH/environment variables or something like that, but every explanation I have read has confused me thus far (Even this one which I thought was going to set me strait Relative imports for the billionth time).
Can someone explain in simple terms what is happening here? What is Pycharm doing by default that other editors are not such that my imported modules are found? How can I make someotherfile.py work in VSCode without modifying it?
Thanks in advance!
Pycharm adds your project directory into python paths by default. See configuration of "Pycharm run" that you execute and you shall see few checkboxes like
If those checked Pycharm creates PYTHONPATH environment variable for you that instructs Python where to look for someclass module.
You will have to configure VSCode to define PYTHONPATH environemnt variable for python command you run and include your root project directory path on it.
TLDR: Mess with the line starting with ./automated pointing it to various directories in your project until it works haha.
Long rambling answer: Alright now that I am not in a frenzy from trying to figure this out and it has been a day, I feel like I can make a conherint response (lets see if that is true).
So my original answer was an attempt to simplify my problem into what I thought was the issue due to a ModuleNotFound error I was getting. I had been trying to get unittests in Python inside of Visual Studio code to work (hint hint just use Pycharm it just works out of the box in there), but the integrated testing feature or whatever could not find my tests giving ModuleNotFound as the reason.
The solution to my problem actually just concerned the line ./automated-software-testsing-with-python/blog.
In the below screenshot the relevant part is ./automated-software-testing-with-python/blog.
This is it when it is correctly configured (My tests work Woo hoo!)
Now you can go ahead and read the official documentation for what this line is doing, but I am convinced that the documentation is wrong. Due to how my code is structured and how my files are named, what the documentation says its looking for definitely does not exist. But that is another can of worms...
I actually got it to work by accident. Originally when you go though the wizard to set up what's in that screenshot above, it defaulted to ./automated-software-testing-with-python which did not work. I then manually edited it to something else that was not the below screenshot hoping to get it to work.
It was only when I pointed it to that blog directory on accident (thinking I was in a different file due to trying to debug this for hours and hours in a blind rage) that it ended up working.
I did a bunch of PYTHONPATH manipulation and Environment Variable mumbo jumbo, and I originally thought that that had an effect, but when I cloned my repot to my other machine (which did not have any of that Environment Variable PYTHONPATH stuff going on) it worked too (again, provided that the line in question pointed to blog.
Ok hopefully that might help somone someday, so I'll end it there. Not to end on a bitter sounding zinger, but I will never cease be amazed by how doing such simple things such as configuring the most basic unit test can be so difficult in our profession haha. Well now I can start working. Thanks for the help to those who answered!

Pylint real time linting on pycharm

Is there any way to use pylint or flake8 real-time inspection on PyCharm? I don't want to push the run bottom but to real-time scan my cod to find the suggestions.
This does not exists as far as I know and for good reasons : if it did it would not be usable because it would be too demanding on the IDE. pylint is checking a lot of thing (for example code duplication which is intrinsically a hard problem to solve) so it's too slow to be run in real time for each key stroke.
I've had great success with the Pylint extension by Roberto Leinardi.
If you're using a virtual environment and have your .pylintrc file in the root of your project, the Pylint extension should pick everything up automatically, even across different platforms. Otherwise, you can go to File > Settings > Pylint to fix them on a per-machine basis.
The only caveat is the "real time" part of your question. I've not been able to get it to run entirely automatically while I'm typing or whatever. Instead I have to save the file I'm currently in (Ctrl + S or switch focus to another window) and then it'll run and make the suggestions in the IDE in the same way the built-in PyCharm linter does.
Hope this helps!

Using Jenkins to run Python script but having issue importing modules on the script

I am really new to Jenkins and Python so when I have initially researched for this problem, there has been a limit to my understanding. I am looking to write a Python script and for it to be run on Jenkins as part of some automated testing I wish to do. My script interacts with an API and hence imports the 'requests' module on Python. It works fine using the Python interpreter on my local machine but I have had issues when I have tried using the Jenkins Python script builder and so I am looking for a way around this.
As I mentioned, I have looked around the internet for solutions but as my knowledge on this topic is limited I have found it difficult to understand certain ideas that have been mentioned on the web. One lead I have had is related to the use of virtual environments on Jenkins, but as its something I've never used, I have struggled implementing it. I have installed the ShiningPanda Plugin on Jenkins, but I am unsure how to use it.
Any help given is greatly appreciated :)
Thanks

Finding unused Django code to remove

I've started working on a project with loads of unused legacy code in it. I was wondering if it might be possible to use a tool like coverage in combination with a crawler (like the django-test-utils one) to help me locate code which isn't getting hit which we can mark with deprecation warnings. I realise that something like this won't be foolproof but thought it might help.
I've tried running coverage.py with the django debug server but it doesn't work correctly (it seems to just profile the runserver machinery rather than my views, etc).
We're improving our test coverage all the time but there's a way to go and I thought there might be a quicker way.
Any thoughts?
Thanks.
You can run the development server under coverage if you use the --noreload switch:
coverage run ./manage.py runserver --noreload
pylint is great tool for static code analysis (among others things it will detect unused imports, variables or arguments).
http://nedbatchelder.com/blog/200806/pylint.html
http://www.doughellmann.com/articles/pythonmagazine/completely-different/2008-03-linters/index.html

Reload mod_fcgid without killing Python Service

I'm currently running a Django project on my school's webserver with FCGI. I did follow the multiple guides that recommends installing a virtual local Python environment and it worked out great. The only issue i had was that "touching" my fcgi-file to reload source-files wasn't enough, but instead i had to kill the python service via SSH. This because mod_fcgid is used.
However, the admin didn't think it was a great idea that i ran my own local python. He thought it better if i just told him what modules to install on root, which was a pretty nice service really.
But doing this, i can no longer kill python since it's under root(though immoral as I am, I've definitely tried). The admins recommendation was that I should try too make the fcgi script reload itself by checking time stamp. I've tried to find documentation on how to do this, but fund very little and since I'm a absolute beginner i have no idea what would work.
Anyone have experience running python/django under mod_fcgid or tips on where to find related guides/documentation?
here's what I would do:
## top of my .fcgi script
import sys, time
original_modules = sys.modules.copy()
## in a separate thread
old_ctime = os.path.getctime("mymodule.py")
while True:
time.sleep(10)
new_ctime = os.path.getctime("mymodule.py")
if new_ctime > old_ctime:
sys.modules = original_modules # reset all imports
import mymodule
mymodule.dofcgi()
granted this isn't drop-in perfect (you might have to mess w/ the threading) but it should give you a general idea of how to "reload" a module completely.

Categories

Resources