Pylint disagrees with file import - python-2.7

In reference to How do I import a Python script from a sibling directory?, Pylint will error with E0401:Unable to import at line 6.
if __name__ == '__main__':
if __package__ is None:
import sys
from os import path
sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))
from components.core import my_module
else:
from ..components.core import my_module
Is this just something that we have to accept, or is there a rigorous solution to correct this?
While the file in question is part of my unit tests and not critical to deployment, I would like to keep everything error free.
(pylint 1.6.4, Python 2.7.12)

If you are referring to importing components, then this would not work, since pylint does not understand modifications of sys.path. It would be theoretically feasible to understand them, but since it is a static analysis tool, it has its limitations with regard to what it can and what it should do. In order to help it, you might try running pylint with --init-hook="import sys; sys.path.append(path.dirname(path.dirname(path.abspath('your file'))))".

Related

Local python module overrides module installed with pip

I am having issues with importing, my own, pip package named the same as a module inside a django-app. Lets say my own pip module is called: fancymodule. I then have a module-folder inside a django-app named the same:
* django-project
* payments
* models.py
* fancymodule/
* __init__.py
The problem I am having, is that inside payments/models.py, I import:
from fancymodule import ApiClient
This is suppose to reference the fancymodule installed through pip. But is instead referencing the module inside the payments-django-app instead.
If I wanted to reference the module inside the payments-django-app, in my head, this should be:
from payments.fancymodule import whatever
I get that from the view of payments/models.py, the import fancymodule .. will reference fancymodule inside the payments-folder.. but can i change/fix this, so it reference the fancymodule installed through pip ?
FYI: Working on an old legacy project.
Home someone can help.
marcinn suggestion about using from __future__ import absolute_import worked.
So my solution inside the models.py file ended up being:
from __future__ import absolute_import
from fancymodule import ApiClient
And as I said to above comment.. I couldn't find anything wrong with my PYTHONPATH. as marcinn also said, this is most likly a Python 3.x thing.. and since i am on 2.7, it makes sense the PYTHONPATH looks fine.
OBS: PyCharm redlines the from fancymodule import ApiClient, but it works. So i think its just PyCharm that needs to be restartet to remove the redlines.

training SyntaxNet from parser_trainer_test.sh (ImportError: cannot import name tf_logging)

I am new to python and SyntaxNet and I am trying to train SyntaxNet from SyntaxNet Tutorial.
To trained the model I updated parser_trainer_test.sh based on The Tutorial. and ran it as follow:
ubuntu#ubuntu-VirtualBox:~/models/syntaxnet$ syntaxnet/parser_trainer_test.sh
It gave me this error:
syntaxnet/parser_trainer_test.sh: line 36: /home/ubuntu/models/syntaxnet/syntaxnet/parser_trainer: No such file or directory
Then I updated "$BINDIR/parser_trainer" \ in line 35 of parser_trainer_test.sh to "$BINDIR/parser_trainer.py" \ and ran it again.
And it gave me this error:
File "/home/ubuntu/models/syntaxnet/syntaxnet/parser_trainer.py", line 25,in <module>
from tensorflow.python.platform import tf_logging as logging
ImportError: cannot import name tf_logging
I went through tf_logging.py and all other python files in syntaxnet/tensorflow/python/platform
I think that's where the error is. But if you want, I can post more.
parser.trainer.py :
import os
import os.path
import time
import tensorflow as tf
from tensorflow.python.platform import tf_logging as logging # this is where error happens
from tensorflow.python.platform import gfile
tf_logging.py:
import logging
import os
import sys
import time
from logging import DEBUG
from logging import ERROR
from logging import FATAL
from logging import INFO
from logging import WARN
There is something that I am suspecious about (I am not perofessional so sorry if it seems silly). I found a file named logging.hin path tensorflow/tensorflow/core/platform which defines DEBUG,ERROR,FATAL,.. and I get confused whether the aim of the model is the logging package from python or this logging.h file. If am wrong, which is probable, please help me through this problem.
I suspect the problem is that you are trying to run the script directly, and it expects to be invoked by Bazel (so that the environment, paths, etc. are set up appropriately). Try the following command instead:
$ bazel test //syntaxnet:parser_trainer_test

No module named os.path : wrong Python being called by bash

OS: CentOS 6.6
Python 2.7
So, I've (re)installed Canopy after it suddenly stopped working after an abrupt shutdown. It worked fine immediately after the install (I installed as my default Python). But after one reboot, when I try to open it with /root/Canopy/canopy (the icon under applications no longer works, either), I get the following error:
(Canopy 64bit) [xxuser#xxlinux ~]$ /root/Canopy/canopy Traceback (most recent call last): File "/home/xxuser/qiime_software/sphinx-1.0.4-release/lib/python2.7/site-packages/site.py", line 73, in <module>
__boot() File "/home/xxuser/qiime_software/sphinx-1.0.4-release/lib/python2.7/site-packages/site.py", line 2, in __boot
import sys, imp, os, os.path ImportError: No module named path
I found this link: Python - os.path doesn't exist: AttributeError: 'module' object has no attribute 'path', but both of my os.py and os.pyc were 250 and 700 bytes, respectively. There was another file called site.py which was 0 bytes and site.pyc was about 100 bytes. What are these files? And would deleting them hurt anything (which is what they did)? And why is this happening after reboot? (using reboot command).
I also found this: https://groups.google.com/forum/#!topic/spyderlib/hKB15JYyLqM , which could be relevant. I've updated my python path before with sys.path.append('/..')
My guess is that for some reason os.path isn't in sys.path? and __boot can't find it? But I'm new to Python and Linux and want to know what I'm doing before I go modifying any boot files, paths, etc.
Thanks in advance.
More information (saw that I'm supposed to update new info in an edit to original question. New to this.)
From one of the comments:
This is what I got:
import os.path
import posixpath
os.path
module 'posixpath' from '/home/xxuser/qiime_software/python-2.7.3-release/lib/python2.7/posixpath.pyc'
posixpath
module 'posixpath' from '/home/xxuser/qiime_software/python-2.7.3-release/lib/python2.7/posixpath.pyc'
Looks like os.path is there.
Could this have to do with a permissions error? I have it installed to /root/Canopy/canopy and I found this: docs.python.org/2/library/os.html#module-os (section 15.1.4). Does that make sense?
I'm also not sure if the following is related, but it might possibly. I can no longer seem to update my path with sys.path.append('/file/path/here'). It works until I close the terminal, then I have to re-append the next time I want to call a module from the new directory. Are sys.path and os.path related in any way?
Just fixed this on OSX with:
brew uninstall python
brew install python
No idea why, never seen it in 5 years of working with Python :S
It turns out that I was onto something with my last comment.
I'd downloaded a bunch of biology modules that depend on python, and so many of them came with their own install. When I added the modules to ~/.bashrc, my bash began calling them in advance of my original CentOS install. Resetting ~/.bashrc and restarting (for some reason source ~/.bashrc didn't work) eliminated all of the extra stuff I'd added to my $PATH and Canopy began working again. I'm going to go through and remove the extra installations of python and hopefully the issue will be behind me. Thanks to everyone who posted answers, especially A.J., because that's what got me thinking about .bashrc .
Edit: With some better understanding, this was all because of using python in a virtual environment. Canopy was resetting my path every time I opened it. I'm using a self-installed virtual environment now and have configured my path.
Try seeing if you have posixpath by typing import posixpath:
>>> import os.path
>>> os.path
<module 'posixpath' from '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>> ^D
bash-3.2$ python
>>> import posixpath
>>> posixpath
<module 'posixpath' from '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>>

Setting current directory for Interactive Console in pydev (Eclipse) at console startup

I want to start an interactive console in pydev from project directory, in order to import an app. I tried to use os.chdir at startup from Window->Preferences->PyDev->Interactive Console->Initial interpreter commands.
I read https://docs.djangoproject.com/en/dev/ref/settings/ searching for an entry to set path but I didn't find anything.
Thanks
Edit: I had to import module first in order to import app
Strange, I must say that after changing the initial interpreter commands to be:
import sys; print('%s %s' % (sys.executable or sys.platform, sys.version))
import os;os.chdir('c:\\')
Later doing (in the shell):
import os.path
os.path.abspath('.')
Does show the expected path... Aren't you getting that? What do you get when you do the abspath('.') in the shell after the startup?

import C++ module, if fail: import Python version?

I have a Python pyd module written in C++. I also have a Python version of the module (which is much slower).
My question is, as I want the program to run on multiple OSs, can I try to import the C++ version in the program and import the slower Python version if that one fails (other OS, architecture)?
Yes, you can import some thing like this:
try:
import CppModule as Module
except ImportError:
import PurePythonModule as Module
Yes you can:
try:
import CppModule
except ImportError:
import PythonModule
Edit: This answer, while not incorrect, is not really useful. As #Best Games' answer shows, this only really useful if you import the module using a common name.