I have few custom modules that i am using across other python scripts, like for eg. my own DB manager. Currently in order to use an existing module, I am inserting path inside my python, which works great but it does fail for pylint.
import sys
sys.path.insert(0, "/home/myuser/Develop/Pipeline/dbconnector")
import dbmanager
Unable to import dbmanager pylint(import-error) [2, 1]
Code works just fine but pylint itself fails. Is it a problem with pylint or is there a more 'correct' way when dealing with custom modules ?
ps. I am still developing those modules to ideally I would love to keep them 'live' as they are so i could quickly tweak on demand
Pylint doesn't execute your code to analyze it, so it can't pick up the fact that you manipulated sys.path that way. You can set your PYTHONPATH environment variable so that the Python interpreter executing Pylint knows where to look.
Related
How do I set up my development environment so my IDE is able to properly resolve imports through odoo.addons?
In some cases in Odoo one may need to import Python objects directly from a file in another custom module. Take a module some_module as an example. In some_module/models/some_model.py there might be an import like so:
from odoo.addons.other_module import some_class
This type of import is done throughout the Odoo source code, and everything is resolved without issues because the built-in Odoo modules are located directly under the odoo/addons directory which has an __init__.py file. However, since my custom modules are in a separate directory, the linter is obviously not able to resolve the imports.
What's the recommended way to deal with this?
If you use PyCharm, you can use the Odoo plugin to help IDE understand Odoo imports and more!
References:
https://odoo-ide.com
https://plugins.jetbrains.com/plugin/13499-odoo
I am working on some old python code and I was wondering if there is an easy way to find out which modules are imported but never used. I have a few hundred python scripts, each importing tens of modules (most scripts where copy-pasted by some template).
Pylint can do this. It reports "Unused import <module>" (warning named unused-import, code W0611).
This warning is enabled by default along with many others, but if you want to check for this warning specifically, you can do:
pylint --disable=all --enable=unused-import *.py
Using Pycharm does inform you if any modules or variables are used or not. That would be good for you in my opinion as an IDE.
I have a python project pypypy with 2 files: __main__.py and foo.py.
In __main__.py I simply do import via import foo. It all works fine.
Now, I want to distribute it with pypi. After installing my module I'm execution it with python -m pypypy. When I do that, the import statement doesn't work anymore. However import pypypy.foo does the job.
Should I change all my imports before distribution or there is a better way?
Using absolute imports is strongly suggested as they work consistently across different python versions. Check this answer. In your case you should prefer using import pypypy.foo.
The reason it works in your dev environment might be because of PYTHONPATH manipulation. For example Pycharm automatically sets Add content and source roots to PYTHONPATH. Also when you run python it automatically adds current working directory to PYTHONPATH.
iPython has this excellent feature of being able to auto-complete modules and this works extremely well in there.
For the longest time, I've had a shell function that allows me to change directories to where a given module is located. It does this by trying to import the name of the module from the first argument and looking into its __file__.
What I would like to do is to find out the importable modules that are available so I can write an auto-complete function for this little helper.
I know that for a give module, I can do something like dir(module_name) and that would give me what I need for that module but I am not sure what to do to find out what I can import, just like iPython when you do something like:
import [TAB]
Or
import St[TAB]
Which would autocomplete all the importable modules that start with St.
I know how to write the auto-completion functionality, I am interesting in just finding the way of knowing what can I import.
EDIT:
Digging through the IPython code I managed to find the exact piece that does the work of getting all of the current modules for the given environment:
from IPython.core.completerlib import module_completion
for module in module_completion('import '):
print module
The reason I'm printing the results is because I am actually using this for ZSH completion so I will need to deal with the output.
As a starting point, you could iterate through the paths in sys.path looking for python modules, as that is exactly what import does. Other than that, Python won’t have an index or something for all modules, as the imports are resolved on run-time, when it’s requested. You could easily provide an index for the built-in modules though and maybe have a cache or something for previously imported modules.
Let's say I make a module called mylib.py. In eclipse I type
import mylib
Then I type mylib. and hit CTRL+SPACE. This should suggest functions/variables in mylib, but it doesn't do anything. If I do something like import os and type os., suggestions immediately pop up, so I know code completion works in general, just not for my modules. Any reason why?
In order to get completion for custom modules, PyDev has to index it (if possible) and introspect the classes, functions, variables and imports defined there. To do so, you should add your module to the eclipse's PYTHONPATH and then reindex your venv (the one defined in PyDev).
Most of the times this is done automatically by the IDE but it doesn't work quite well (at least it is not perfect).
I really suggest you not to rely at 100% on the IDE completion.