Pylint and python - python

I want to bind a file containing pylint disable commands into a python file. and run it using pylint command.
like pylint --reports=n filename.py
but how to bind and external file containing(#pylint: disable=(code)) pylint disable command into filename.py
anyone knows please comment your answers

First, create a Pylint configuration file:
pylint --rcfile=/path/to/config.file
then put this in your config file with what commands you want to disable after the "disable=" part:
disable=

Related

shebang not working for python script

I've been looking over many answers here on stackoverflow. I've tried absolutely everything. I have this at the top of my AddressConversion.py python script.
#!/usr/bin/env python
import argparse
The objective is to run this as a command utility, meaning I could type
AddressConversion [options][address]
As of now I would settle for being able to type
./AddressConversion [options][address]
I have done the chmod so the file is executable
I've ran dos2unix on the file to eliminate any random windows characters(which wouldn't seem possible because the file has only been used on Ubuntu.
I've checked the python install with which python with the results
/usr/bin/python
I've also checked which env and get a similar path
The script will work fine when I use the traditional python command. It also works fine when I type:
usr/bin/env python
It will open up the python interpreter.
These steps seem to be the solutions suggested anytime someone asks this question. I am getting this error:
./AddressConversion.py: line 1: import: command not found
./AddressConversion.py: line 3: syntax error near unexpected token `('
./AddressConversion.py: line 3: `def init_parser():'
which seems like it is trying to run it as a shell script or something.
Any suggestions?
created one file executeme.py
#!/usr/bin/env python
print("hello")
make it as executable (optional)
chmod a+x executeme.py
reanme the file
mv executeme.py executeme
Execute now
./executeme
OUTPUT
hello
Another option to create one setup.py file, for more you can read here
in entry_points a key name console_script in which you can give the name of executor and target module in format
'name=target'
from setuptools import setup, find_packages
setup(
name='executor',
packages=find_packages(),
install_requires=[,
],
entry_points = {
'console_scripts': [
'executeme=executeme:main',
],
},
)
then run the command
pip install -e /path/to/setup.py
Installing from local src in Development Mode, i.e. in such a way that
the project appears to be installed, but yet is still editable from
the src tree.
pipdoc
I had a similar issue and it ended being because of the CRLF at the end of lines. These were added when the script was created on a windows machine. To check if this is the case use the file command.
file script.py
It will give you an output like this. "Python script, ASCII text executable, with CRLF line terminators"
To remove the CRLF line terminators do the following.
dos2unix script.py

Pylint. How to disable graph?

I'm using pylint for code analysis. When I run pylint I see long output about dependencies. It looks like this:
External dependencies
---------------------
::
example_lib (my_project.pack1.name_module)
pack1 (pack1.pack2.name_module1,pack1.pack2.name_module2,...)
\-...
... # long list of dependencies here
How can I disable this graph? Does anyone know some parameter for pylint config to disable External dependencies output?
Thank you.
You want to disable report RP0401. You can do this on the command line with pylint -d RP0401, or in .pylintrc append 'RP0401' to the list on the line beginning disable= (in section [MESSAGES CONTROL]).

Intellij/Pycharm can't debug Python modules

I use PyCharm/IntelliJ community editions from a wile to write and debug Python scripts, but now I'm trying to debug a Python module, and PyCharm does a wrong command line instruction parsing, causing an execution error, or maybe I'm making a bad configuration.
This is my run/debug configuration:
And this is executed when I run the module (no problems here):
/usr/bin/python3.4 -m histraw
But when I debug, this is the output in the IntelliJ console:
/usr/bin/python3.4 -m /opt/apps/pycharm/helpers/pydev/pydevd.py --multiproc --client 127.0.0.1 --port 57851 --file histraw
/usr/bin/python3.4: Error while finding spec for '/opt/apps/pycharm/helpers/pydev/pydevd.py' (<class 'ImportError'>: No module named '/opt/apps/pycharm/helpers/pydev/pydevd')
Process finished with exit code 1
As you can see, the parameters are wrong parsed, and after -m option a IntelliJ debug script is passed before the module name.
I also tried just put -m histraw in the Script field, but doesn't work, that field is only to put Python script paths, not modules.
Any ideas?
There is another way to make it work.You can write a python script to run your module.Then just configure PyCharm to run this script.
import sys
import os
import runpy
path = os.path.dirname(sys.modules[__name__].__file__)
path = os.path.join(path, '..')
sys.path.insert(0, path)
runpy.run_module('<your module name>', run_name="__main__",alter_sys=True)
Then the debugger works.
In PyCharm 2019.1 (professional), I'm able to select run as module option under configurations, as below
I found it easiest to create a bootstrap file (debuglaunch.py) with the following contents.
from {package} import {file with __main__}
if __name__ == '__main__':
{file with __main__}.main()
For example, to launch locustio in the pycharm debugger, I created debuglaunch.py like this:
from locust import main
if __name__ == '__main__':
main.main()
And configured pycharm as follows.
NOTE: I found I was not able to break into the debugger unless I added a breakpoint on main.main() . That may be specific to locustio, however.
The problem is already fixed since PyCharm 4.5.2. See corresponding issue in PyCharm tracker:
https://youtrack.jetbrains.com/issue/PY-15230

How do I make a python script executable?

How can I run a python script with my own command line name like myscript without having to do python myscript.py in the terminal?
Add a shebang line to the top of the script:
#!/usr/bin/env python
Mark the script as executable:
chmod +x myscript.py
Add the dir containing it to your PATH variable. (If you want it to stick, you'll have to do this in .bashrc or .bash_profile in your home dir.)
export PATH=/path/to/script:$PATH
The best way, which is cross-platform, is to create setup.py, define an entry point in it and install with pip.
Say you have the following contents of myscript.py:
def run():
print('Hello world')
Then you add setup.py with the following:
from setuptools import setup
setup(
name='myscript',
version='0.0.1',
entry_points={
'console_scripts': [
'myscript=myscript:run'
]
}
)
Entry point format is terminal_command_name=python_script_name:main_method_name
Finally install with the following command.
pip install -e /path/to/script/folder
-e stands for editable, meaning you'll be able to work on the script and invoke the latest version without need to reinstall
After that you can run myscript from any directory.
I usually do in the script:
#!/usr/bin/python
... code ...
And in terminal:
$: chmod 755 yourfile.py
$: ./yourfile.py
Another related solution which some people may be interested in. One can also directly embed the contents of myscript.py into your .bashrc file on Linux (should also work for MacOS I think)
For example, I have the following function defined in my .bashrc for dumping Python pickles to the terminal, note that the ${1} is the first argument following the function name:
depickle() {
python << EOPYTHON
import pickle
f = open('${1}', 'rb')
while True:
try:
print(pickle.load(f))
except EOFError:
break
EOPYTHON
}
With this in place (and after reloading .bashrc), I can now run depickle a.pickle from any terminal or directory on my computer.
The simplest way that comes to my mind is to use "pyinstaller".
create an environment that contains all the lib you have used in your code.
activate the environment and in the command window write pip install pyinstaller
Use the command window to open the main directory that codes maincode.py is located.
remember to keep the environment active and write pyinstaller maincode.py
Check the folder named "build" and you will find the executable file.
I hope that this solution helps you.
GL
I've struggled for a few days with the problem of not finding the command py -3 or any other related to pylauncher command if script was running by service created using Nssm tool.
But same commands worked when run directly from cmd.
What was the solution? Just to re-run Python installer and at the very end click the option to disable path length limit.
I'll just leave it here, so that anyone can use this answer and find it helpful.

Python correctness (i.e., lint) analyzing for Notepad++

Does anyone know of anything like pylint or pychecker for notepad++? Or perhaps how to use pylint in notepad++.
If you install the Python Script plugin, then you can add a new script with the following lines to get pretty good results:
console.show()
console.clear()
console.run('cmd.exe /c '
+ 'C:\\Python26\\Scripts\\pylint.bat --reports=n -f parseable '
+ '"%s"' % notepad.getCurrentFilename())
The output will include hyperlinks to the lines with the errors/warnings (if the filenames don't have spaces in them...)
The option "-f parseable" is deprecated in the current version of Pylint.
The current equivalent alternative is:
console.run('cmd.exe /c '
+ 'C:\\Python26\\Scripts\\pylint.bat --reports=n '
+ '--msg-template="%s" %s'
% ( '{path}:{line}: {msg_id}({symbol}), {obj} {msg}', notepad.getCurrentFilename()))
Note: python path can be different e.g. C:\\Python27.
Note2: double quotes in --msg-template="..." are important
You could install PyLint using python -m pip install pylint and use it via Notepad++'s Run... command (F5):
cmd /c python -m pylint "$(FULL_CURRENT_PATH)" & pause
To get the output in Notepad++ and link to the code, use NppExec.
None of the other answers worked for me, but this does:
Install PyLint using python -m pip install pylint
Install NppExec via the Plugin Manager, press F6, and save this script as "PyLint":
NPP_SAVE
cd "$(FULL_CURRENT_PATH)"
env_set PYTHONIOENCODING=utf-8
python -u -m pylint "$(FULL_CURRENT_PATH)"
Sample output:
NPP_SAVE: C:\Users\Cees\Documents\http_ear.py
CD: C:\Users\Cees\Documents\http_ear.py
Current directory: C:\Users\Cees\Documents
ENV_SET: PYTHONIOENCODING=utf-8
$(SYS.PYTHONIOENCODING) = utf-8
python -u -m pylint "C:\Users\Cees\Documents\http_ear.py"
Process started (PID=25136) >>>
************* Module http_ear
http_ear.py:16:0: C0301: Line too long (1780/100) (line-too-long)
http_ear.py:17:0: C0301: Line too long (226/100) (line-too-long)
http_ear.py:26:0: C0304: Final newline missing (missing-final-newline)
------------------------------------------------------------------
Your code has been rated at 8.00/10 (previous run: 8.00/10, +0.00)
<<< Process finished (PID=25136). (Exit code 16)
================ READY ================
You can link the bug locations using NppExec's Console Output Filters. Press Shift+F6 and enable this filter with Red set to FF:
%FILE%:%LINE%:%CHAR%
Then double clicking a red line focuses the specified location in the editor.
You should use the Executable instead of the Batch if you want to use Pylint within NotePad++.
Go to the Configuration from Python Script and create a new .py File to run Pylint from that. (i called my file npphelper.py)
(Add that npphelper.py to Menu-items and Toolbar-icons, then you can execute it by pressing a Button.)
This will run Pylint into Notepad++, i splitted the Command into 2 Parts:
pyLint = 'C:\\PROGRA~1\\Python35\\Scripts\\pylint.exe --reports=n'
console.show()
console.clear()
console.run('%s "%s"' % (pyLint, notepad.getCurrentFilename()))
Path to pylint.exe (i used a Shortname instead of Doublequotes)
The File you want to check with Pylint (actualy returns the Path from active Tab)
(You have to change the Paths so that it fits to your Installation...)
All you have to do now is saving this npphelper.py, open the Tab with your Project-File and run the npphelper.py you created for pylint. (e.g. via button)
If you don't want to use the default Configuration then generate a pylintrc Template (save them where you want). I've done it via CMD with the following Command:
pylint.exe --generate-rcfile>>myfilename.pylintrc
Then you need to change some lines into the npphelper.py:
rcfile = 'C:\\PROGRA~1\\Python35\\Scripts\\myrcfile.pylintrc'
pyLint = 'C:\\PROGRA~1\\Python35\\Scripts\\pylint.exe --reports=n --rcfile="%s"' % rcfile
console.show()
console.clear()
console.run('%s "%s"' % (pyLint, notepad.getCurrentFilename()))
I've installed Python Script 1.0.8.0 with all the Extras using the .msi File here.
(Using the PluginManager in Notepad++ gives you version 1.0.6.0 instead of 1.0.8.0)
I use Windows 7 with Notepad++ 6.9.1, Python 3.5.1 and Pylint 1.5.5.
(i installed pylint via CMD -> "pip install pylint" and updated it.)
Some more usefull Links:
How do I create a pylintrc file
PyLint “Unable to import” error - how to set PYTHONPATH?

Categories