pylint complains about empty __init__.py - python

I get this weird error:
$ epylint .
*************
mysubdir/__init__.py:1: fatal (F0010, parse-error, ) error while code parsing: Unable to load file 'mysubdir/__init__.py' ([Errno 2] No such file or directory: 'mysubdir/__init__.py')
Note that mysubdir/__init__.py does exist and contains a single empty line.
Note that
$ epylint mysubdir
works just fine and produces no messages.
What am I doing wrong?

The problem is you're running pylint in your workdir (pylint .). Pylint will expect an init.py file there.
Take a look at this discussion.
Your best shot is to select the directories you want to lint. If you can't do so (ie: Django project). You'll have to make a custom workaround like in the thread.

Related

How to use pipreqs to create requirements.txt file

I am trying to use the pipreqs package to generate a requirements.txt file. Following the documentation's examples I have been unable to make it work.
It seems simple enough, the documentation tells me to use this command in the terminal:
$ pipreqs /home/project/location
In my case, this is the location of my project:
$ pipreqs /Users/ep9k/Desktop/UVA\ Big\Deal
And I get this error message:
FileNotFoundError: [Errno 2] No such file or directory: '/Users/ep9k/Desktop/UVA BigDeal/requirements.txt'
The error message is correct, there is no requirements.txt file in that directory. I am trying to use pipreqs to create the requirements.txt file.
Reading the documentation, pipreqs is supposed to scan a python file for import statements and build the requirements.txt document based on that. So I tried this as well, with the file I want pipreqs to scan:
$ pipreqs /Users/ep9k/Desktop/UVA\ Big\Deal/1FigrFunctions.py
and I received this error, which is pretty much the same as above:
FileNotFoundError: [Errno 2] No such file or directory: '/Users/ep9k/Desktop/UVA BigDeal/1FigrFunctions.py/requirements.txt'
I am sure the solution is obvious. What am I doing wrong?
I answered my own question. I think the errors were due to there being a bunch of files in the directory I was trying to run pipreqs on. To solve this problem, I created a new empty directory called "test" and put the file I wanted to generate requirements.txt for in that directory. So there is only one file in my new "test" directory. Then I ran the command as follows:
$ pipreqs /Users/ep9k/Desktop/test
and it worked. Really simple...
I discovered how to solve this error using below steps-
If you want to specify full path for your python file, use this -
pipreqs --encoding utf-8 "C:\Users\username\Downloads\Bl_container\training\fold\"
Also, the python file should be alone in the folder.
If you are currently in your python file directory, use this -
pipreqs --encoding utf-8 "./"
Using encoding to get rid of the map error.
Make sure that you are not connected to any VPN or you may get HTTPConnection error.
Have you tried:
$ pipreqs --encoding utf-8 "/Users/ep9k/Desktop/UVA/Big/Deal" --force
I've managed to create requirement.txt for multiple .py files in one dir.
Use Pigar instead, i have a lot of problems with pipreqs
https://github.com/Damnever/pigar
pip install pigar

Packaging local module with pex

I'm trying to package my local module with pex and I can't seem to manage to.
I've created a simple project:
→ python --version
Python 2.7.10
→ pex --version
pex 1.1.15
→ tree .
.
├── bla
│   ├── __init__.py
│   └── main.py
└── setup.py
bla/__init__.py
import main
bla/main.py
if __name__ == '__main__':
print 'yo'
Which to me seems like the simplest project possible.
→ pex -v . -o v.pex --disable-cache
Traceback (most recent call last):
File "/Users/Charly/repos/load_tester/venv/bin/pex", line 11, in <module>
sys.exit(main())
File "/Users/Charly/repos/load_tester/venv/lib/python2.7/site-packages/pex/bin/pex.py", line 540, in main
pex_builder = build_pex(reqs, options, resolver_options_builder)
File "/Users/Charly/repos/load_tester/venv/lib/python2.7/site-packages/pex/bin/pex.py", line 475, in build_pex
resolvables = [Resolvable.get(arg, resolver_option_builder) for arg in args]
File "/Users/Charly/repos/load_tester/venv/lib/python2.7/site-packages/pex/resolvable.py", line 61, in get
raise cls.InvalidRequirement('Unknown requirement type: %s' % resolvable_string)
pex.resolvable.InvalidRequirement: Unknown requirement type: .
Also tried to do python setup.py bdist_pex but this failed as well as it can't find the command.
It really seems like I'm misunderstanding something fundamental but I can't figure out what.
I recently had a bit of a fight with pex trying to make it include local modules. What I learned is:
You must provide a valid setup.py file for your module(s) in order for this to work, and:
You must specify the application's entry point
This was tricky to figure out for several reasons. From reading the documentation, I was able to infer that the correct command in my case should be something like this:
$ pex . -v -e usersnotifier:main -o usersnotifier.pex
However, when I tried this, I kept getting an error saying:
pex.resolvable.InvalidRequirement: Unknown requirement type: .
A web search for this error turns up—as its first hit—this Github issue, which is still open as I type this. A spent a long while thinking that the above command wasn't working because of this bug. I attempted to downgrade setuptools and made a half-dozen other fruitless attempts to 'fix' the problem before this SO answer hinted at the necessity of supplying a setup.py file. (That Github issue turned out to be a red herring. The setuptools bug it mentions has since been fixed, from what I can tell.)
So... I wrote a setup.py file. At first, I kept getting that error saying Unknown requirement type: . But then I realized that my setup.py simply contained a dead-obvious typographical error. The error message emitted by pex in this case was actually quite clear, but it was followed by a large-ish stack trace and the Unknown requirement type: . message. I just wasn't paying close attention and missed it for longer than I care to admit.
I finally noticed my typo and fixed it, but another flaw in my setup.py was failing to include my local modules. pex worked in this case, but the generated file didn't:
$ pex . -v -e usersnotifier:main -o usersnotifier.pex --disable-cache
usersnotifier 0.1: Resolving distributions :: Packaging paho-mqtt
pyinotify 0.9.6
paho-mqtt 1.3.1
pex: Building pex: 2704.3ms
pex: Resolving distributions: 2393.2ms
pex: Packaging usersnotifier: 319.3ms
pex: Packaging pyinotify: 347.4ms
pex: Packaging paho-mqtt: 361.1ms
Saving PEX file to usersnotifier.pex
$ ./usersnotifier.pex
Traceback (most recent call last):
File ".bootstrap/_pex/pex.py", line 367, in execute
File ".bootstrap/_pex/pex.py", line 293, in _wrap_coverage
File ".bootstrap/_pex/pex.py", line 325, in _wrap_profiling
File ".bootstrap/_pex/pex.py", line 410, in _execute
File ".bootstrap/_pex/pex.py", line 468, in execute_entry
File ".bootstrap/_pex/pex.py", line 482, in execute_pkg_resources
File ".bootstrap/pkg_resources/__init__.py", line 2297, in resolve
ImportError: No module named 'usersnotifier'
Here's the bare-bones setup.py that finally worked for me:
from setuptools import setup
setup(
name='usersnotifier',
version='0.1',
py_modules=['usersnotifier', 'userswatcher'],
install_requires=[
'paho-mqtt>=1.3.1',
'pyinotify>=0.9.6',
],
include_package_data=True,
zip_safe=False
)
The reason it hadn't worked before was that I was accidentally passing the parameter py_module to setup() rather than py_modules (plural). ¯\_(ツ)_/¯
The final hurdle I encountered was mentioned in #cmcginty's answer to this question, namely: unless your module version number changes, pex will cache/reuse artifacts from the last time you ran it. So, if you fix a problem in your setup.py and re-run pex, it won't actually incorporate your changes unless you: a) bump the version number, or b) pass --disable-cache when invoking pex.
At the end of the day, the whole thing turned into an exercise in writing a proper setup.py, and running:
$ pex . -v -e mymodule:main -o mymodule.pex --disable-cache
Here are a few tips I can offer (possibly to a future version of my self):
TIP 1
Use python setup.py sdist to test your setup.py file. It's surprisingly easy to screw this up, and there's no point involving pex until you're sure your package has the right contents. After running python setup.py sdist, try installing the source package it generates (located in the dist folder) into a fresh venv and see whether it contains all the files you expect. Only move on to invoking pex after this is working.
TIP 2
Always pass --disable-cache to pex unless you have a good reason not to.
TIP 3
While troubleshooting all of these issues, I discovered that I could run:
$ unzip mymodule.pex
to extract the contents of the PEX file. This can be helpful in resolving any discrepancies that remain between your sdist package contents and your pex-ified application.
one way to do is:
create create a source distribution (tarball, zip file, etc.) using python setup.py sdist
then run pex command with -f DIST_DIR switch
eg. pex $(pip freeze) -o aflaskapp.pex -e 'aflaskapp.app' -f dist -v
Some additional details I discovered. If you run:
pex .
It will attempt to use the setup.py build in the directory. If you do not have that, then pex will fail.
By default, pex will cache the local package output, so if you change your setup.py then use the command to make sure your the changes are applied on the next pex run.
pex . --disable-cache

Import statement works on PyCharm but not from terminal

PyCharm 2016.2.3, Mac OS X 10.11.1, Python 3.5 (Homebrew);
I have this folder structure
project
/somepackage
/subpackage
__init__.py
bar.py
__init__.py
foo.py
foo.py:
import somepackage.subpackage.bar
print("foo")
bar.py:
print("bar")
So my expected output is
bar
foo
This works fine when run from PyCharm. However, when I run it from my terminal I get an ImportError:
$ pwd
$ /home/project (not the actual path; just omitting some personal stuff)
$ python3.5 somepackage/foo.py
File "foo.py", line 1, in <module>
import somepackage.subpackage.bar
ImportError: No module named 'somepackage'
I have found this question, which is about the same problem. However, none of the suggested solutions work for me, as I am indeed using the same Python interpreter as PyCharm does and I am currently in the folder that contains the /somepackage folder.
Does anyone have any other suggestions about how to solve this issue?
You are running foo.py like a script, but you are really using it like a module. So the proper solution is to run it as a module:
python3 -m somepackage.foo
For the record, another alternative is to edit your path like:
export PYTHONPATH=.
(Or you could put the absolute directory in there, and of course you should append any other directories that are already in your PYTHONPATH.) This is closer to what PyCharm does, but is less philosophically correct.
Setting PYTHONPATH is what makes it work, as noted above. I use the following VSCODE .env content so that it works for any project:
PYTHONPATH=${PROJ_DIR}:${PYTHONPATH}
This is essentially what PyCharm does when you check "Add Content Roots to PYTHONPATH" in your run/debug configuration. It's a helpful setting, but it spoils you because your code fails outside PyCharm.
Or, if you run in terminal, first export:
export PYTHONPATH=...
Took me days to work all this out.
i solved my problem by two steps on Linux:
first step
go to the root directory of your project and set:
export PYTHONPATH=$PATHONPATH:`pwd`
second step
run python3 -m somepackage.foo
remember Without '.py' suffix
I just had the same problem using scapy.layers.http Module,
this problem occurred on my Kali (linux-Debian) but run fine on Win-10 (after few modifications.)
packet was installed (scapy-http) correctly and the program was running in PyCharm but not as script (from terminal)
I tried solving it with reinstalling in main root, and messing with the sys.path but None have worked.
Troubleshoot & Solution
I found that it looks for the http module in:
/usr/local/lib/python3.7/dist-packages/scapy/layers/init.py
and got the ImportError:
from scapy.layers import http --->
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'http' from 'scapy.layers' (/usr/local/lib/python3.7/dist-packages/scapy/layers/__init__.py)
So then I checked where scapy-http is really installed, which is module called http.py, so I just copied it to:
/usr/local/lib/python3.7/dist-packages/scapy/layers/
found the http.py file in :/usr/local/lib/python3.7/dist-packages/scapy_http/
And that did it :)!
I know its a bit glitchie but that worked!

pylint with jenkins - complince that can't find xml file

I am trying to run pylint with jenkins with following command:
pylint -f parseable -d I0011,R0801 "mypath\highLevel" | tee.exe pylint.out
The process looks run fine, pylint.out created with a lot of information inside but during pylint report creation I get following error:
13:38:27 ERROR: Publisher hudson.plugins.violations.ViolationsPublisher aborted due to exception
13:38:27 java.io.FileNotFoundException: C:\Users\DMD\.jenkins\jobs\Diamond - Run Coverage\builds\2015-07-26_13-34-30\violations\file\A:\highLevel\Monitor\InitialBootAdapter.py.xml (The filename, directory name, or volume label syntax is incorrect)
It's creates very strange path:
C:\Users\DMD\.jenkins\jobs\Diamond - Run Coverage\builds\2015-07-26_13-34-30\violations\file\A:\highLevel\Monitor\InitialBootAdapter.py.xml
I don't really understand what happens.
Why pylint is interested in file InitialBootAdapter.py? Why it's looks for file InitialBootAdapter.py.xml? Who should create it and why? I searched for this file over all the environment and didn't find. But I did'nt find any xml for my other py files?
Maybe you have experience with pylint and can help?
Thank you.
I have experience with pylint in jenkins. And here is how I use it, hope it will help someone.
Step 1
Add a "Execute Shell" step and execute the pylint command to generate the pylint.out. Please note
/usr/local/bin/pylint -f parseable -d I0011,R0801 my-python-project-folder | tee pylint.out
Step 2
Make sure you have the Violation Report Plugin, after that , click Add post-build action-->Report Violation, put the pylint.out in the corresponding field.
And after the successful run, the pylint report looks like this:
I fixed the problem, it took time and DevOps help but it worked and is described in my own blog (it's more my online notebook than blog) in very small details.
The most important point in this post is small utility
import fileinput, sys
if __name__ == "__main__":
for line in fileinput.FileInput(sys.argv[1], inplace=True):
if ".cs" in line:
line = line.replace("\\", "/")
print line,
Here sys.argv[1] should be path to your violations.xml file.
You have to move the path as a command line argument to the utility as path to your violations.xml file is dynamic and depends on build id.

Received 'can't find '__main__' module in '<packageName>' with python package

I'm trying to release my first Python package in the wild and I was successful in setting it up on PyPi and able to do a pip install. When I try to run the package via the command line ($ python etlTest), I receive the following error:
/usr/bin/python: can't find '__main__' module in 'etlTest'
When I run the code directly from my IDE, it works without issue. I am using Python 2.7 and have __init__.py scripts where required. What do I need to do to get this working?
I can easily replicate your problem, actually even without using your package:
$ python empty
.env/bin/python: can't open file 'empty': [Errno 2] No such file or directory
$ mkdir empty
$ python empty
.env/bin/python: can't find '__main__' module in 'empty'
$ python Empty
.env/bin/python: can't find '__main__' module in 'Empty'
So you are not calling your library at all, you are just giving the Python interpreter a nonexistent script name, which in case there is a like-named directory (case-insensitive even) in the working directory it tries to execute it.
I was able to install your package from PyPi just fine and can import it alright, but there doesn't seem to be any entry point to it, nothing useful is exported at the top level and no executable scripts are added to Python bin:
>>> import etltest
>>> dir(etltest)
['__author__', '__builtins__', '__doc__', '__file__', '__name__', '__package__',
'__path__', '__version__']
Running the etlTest.py directly as suggested in Quickstart from your docs doesn't work either:
$ python .env/lib/python2.7/site-packages/etltest/etlTest.py
2014-07-13 17:19:56,831 - settings - DEBUG - Attempting to load .env/lib/python2.7/site-packages/.etltest-settings.yml
2014-07-13 17:19:56,832 - settings - WARNING - No such file or directory .env/lib/python2.7/site-packages/.etltest-settings.yml
2014-07-13 17:19:56,832 - settings - DEBUG - Attempting to load .env/lib/python2.7/site-packages/.etltest-settings.yml
2014-07-13 17:19:56,832 - settings - WARNING - No such file or directory .env/lib/python2.7/site-packages/.etltest-settings.yml
2014-07-13 17:19:56,832 - settings - WARNING - Could not find settings file in .env/lib/python2.7/site-packages/.etltest-settings.yml,.env/lib/python2.7/site-packages/.etltest-settings.yml. Using defaults where present.
Traceback (most recent call last):
File ".env/lib/python2.7/site-packages/etltest/etlTest.py", line 73, in <module>
main(sys.argv[1:])
File ".env/lib/python2.7/site-packages/etltest/etlTest.py", line 22, in main
SettingsManager().first_run_test()
File ".env/lib/python2.7/site-packages/etltest/utilities/settings_manager.py", line 29, in __init__
self.app_name = etltest_config['app_name']
KeyError: 'app_name'
I'd say your package is not apt for ditribution yet. You might want to clean up some dependencies on your development environment and read upon setuptools entry points to provide proper command line executables along with your package. Also it shouldn't run in debug mode by default if it's not some kind of a testing release.
I recently got the same issue and finaly find the solution by my self.
So as the error said, I added a file __main__.py at the same path that my __init__.py.
Inside __main__.py I added the following code :
from mypackage.mymodule import main
main()
main() was the main function of my code. And it's works now.
here my directory:
package
|__dirpackage
|_mypackage.py
|_ __init__.py
|_ __main__.py
|_setup.py`
Just change the name __init__.py file to __main__.py
I had the same problem and solved it by making sure I'm in the correct directory of the package you are trying to run.
For Windows, type dir in the console, while on Linux/macOS - ls to see your current directory

Categories

Resources