Pylint does not recognise PYTHONPATH in VScode - python

I'm sure I'm doing something wrong(i.e. stupid), but running
from stuff.modules import maths
Pylint just keeps giving
No name 'modules' in module 'stuff' pylint(no-name-in-module)
Unable to import 'stuff.modules' pylint(import-error)
My stuff-module is located at /home/abb, so I tried to add both PYTHONPATH="/home/abb" and PYTHONPATH="/home/abb/stuff" to an .env-file, but since it still gives the errors, I think the problem is that I'm also using the stuff-folder as my workspace.
I don't think it's a problem with the .env-file, since I'm also importing from my lib-module located in the same folder as stuff, and this doesn't raise any errors.
(Oh, and if it is important, I'm running this on an SSH-connection)

I had a similar issue. I did 'fix' it by faking another path by using ProjectRootFolder.stuff.modules, saving, and typing again stuff.modules.
I know that it was not a real fix, but from then it worked, also it is stands ever marked with a red under-line.
I explained my issue here.

I had to edit python.analysis.extraPaths.
I tried the .env route and could not get it to work. I actually spent a couple hours on it.
If you go file->preferences->settings or ctrl+, you can search for "extrapath".
Just add your /home/abb/stuff there and it should work.

Related

ModuleNotFoundError even though the module is recognized

My folder structure is:
|-fastapi
|-app
|-calc.py
|-tests
|-mytest.py
In mytest.py I'm trying to import calc.py, like this:
from app import calc
In mytest.py, app and calc are both highlighted green, and when I hover over them, it says (module). It seems to be recognized, but when I run it, I get the error. I know this has been asked before but I haven't found the solution.
I create the exact same file hierarchy in my machine and then made a random function called hello() in the calc.py file, this is the code which successfully calls the function in app folder,
import sys
sys.path.append('../')
from app import calc
print(calc.hello())
maybe I misunderstood the question so here's a much detailed answer with respect to behaviour of the IDE when importing a module,
as we can see in the image below, we can import a wrong module in almost any python IDE and it won't give an error until or unless it is a smart IDE with preinstalled extensions to check as it does for other scripting languages or it won't give an error just yet, below it can be seen,
we can see that IDE doesn't show any error just yet doesn't even give a warning of any sort, but when you run the code you get this error, this is the same error which OP gets when they run their code,
this can be tried for from app import calc and it will give you same error as it gives for from imagination import dreams.
but when you add couple lines of code which are,
import sys
sys.path.append('../')
it starts to import in a similar way which OP was trying to do but only with the additional lines of code, why?
This is because they have their files in parallel to each other, which can be seen in the folders which they mention,
|-fastapi
|-app
|-calc.py
|-tests
|-mytest.py
perhaps this answers the concern which a comment mentions, I took the liberty to assume a few things but more or less this is what's happening, please rectify this answer.
You need __init__.py in each folder to mark it as a package. It can be an empty file. Check the docs for more information.
You should check and ensure you do not have an already existing file named 'calc.py'.

Ride.py shows Calling method 'start_keyword' of listener XXX failed

I'm trying to run an automated test in Ride.py. This test works on my colleague's computer but for some reason does not work on mine. The test starts but at a certain point i get the following error:
[ ERROR ] Calling method 'start_keyword' of listener 'C:\Python27\lib\site-packages\robotide\contrib\testrunner\TestRunnerAgent.py' failed: IndexError: list index out of range
The interesting part is that this error occurs on the same spot ever time, but with a different test it happens at a different time.
I tried to google several things and nothing worked. One solution suggested there was a '#' commented somewhere and this caused the crash. I looked but I don't see a '#' commented anywhere.
Another suggestion lead me to believe my testrunneragent.py file must have been installed wrong. I went online to find the file and replaced it. This did not work either (reran the test before and after a restart of ride)
We tried to re-import the test files thinking perhaps something went wrong there. This did not help either.
Googling juts the last part (IndexError: list index out of range) gave me the suggestion it does not recognize all the lines of code in the back-end file. I would have no clue how to solve this as im not a major coder.
One difference between me and my colleague could be the versions. I downloaded python version 2.7.16 and ride 1.7.3.1. My colleague uses an older version of both python and RIDE. Perhaps the problem could be here?
https://paste.fedoraproject.org/paste/TLekH3az0m4wuUyM8C2RYw
I expect the test will run without failing (it is a happy flow) I have included some screenshots with code in the previous segment that might help
downgraded to the same version of Ride.py
This seems to have fixed the issue

Properly Resolving Import Errors with Directories

My question here is more about the appropriate way to resolve import errors in a way that is considered "Pythonically correct," if there is such a term. I thought I had solution to my problem (posted below and updated), but even that turns out not work.
Context
I have looked at various responses to this (like How to resolve import errors in python?) and I think I've done those things, but I still have the question that I'm posting here.
Example
I have the following structure:
pacumen
rules
__init__.py
pacman_rules.py
pacman.py
game.py
In pacman_rules.py I have a class:
class PacmanRules:
....
In my pacman.py file, I have the following import statement:
from rules.pacman_rules import PacmanRules
Mind you, this was auto-generated by PyCharm. So PyCharm shows me that it resolves just fine.
However, when I run pacman.py from the command line, I get the following error:
ImportError: cannot import name PacmanRules
I have no idea why that's happening in this case, given the lines of code.
In pacman.py I also have this statement:
from game import GameStateData
That one is working just fine. Notice that the class GameStateData comes from my game.py file. So I'm guessing my issue is that I'm trying to import from a file in a different directory rather than from a file in the same directory.
Possible Solution (Update: Does Not Work)
So then I tried this import instead:
from rules import pacman_rules
That seems to work, but it requires me to change lines like this:
PacmanRules.getLegalActions(self)
to lines like this:
pacman_rules.PacmanRules.getLegalActions(self)
That just feels messy to me and seems to defeat the purpose of importing a class name so I can use it without having to preface it.
But, as it turns out, this doesn't work either. This just gets a different error:
ImportError: cannot import name pacman_rules
So that's my question here. Most questions seem to revolve around how to fix the issue; as it turns out I don't even have a "fix."
What I'm curious about is why the initial approach doesn't work (especially since PyCharm generates it) and if my proposed solution is the appropriate way to handle this.

Getting "unresolved reference: print" in pycharm community version 2016.1.4

I have already tried "invalidate cache and restart" (as found here).
And I have already tried turning on "collect run-time types for information for code insight" (as found here).
So I decided to simply remove the error by "ignore unresolved reference" on the print() statements that were complaining (I am not using print 'hello', I am using the function properly).
Now it seems that even actual errors will not get caught, I type in:
prant('hello')
and it doesn't even register the misspelling of "print".
I have my project interpreter set to a version in miniconda environment, which I don't think would change anything, but even changing back to the default version will not get rid of the errors.
In the attached image you can see that "prant('hello') is simply not even registering as an unresolved reference now.
Well, I "fixed" it, although I don't usually consider completely uninstalling the software, then having to delete settings from C:\Users\myname (which weren't caught by uninstall.exe), then making a new directory, creating new .py files and manually copy+pasting the contents of the old ones into it, a "fix".
But on the plus side, it actually does seem to find all my custom imports no problem, which it never did before, and now it actually catches unresolved references properly. It's just a shame I had to go full nuclear just to make it happen. My guess is I FUBAR'd the settings somehow and it kept persisting because those folders in C:\users\myname just wouldn't go away until I deleted them manually.
By the way, I tried both of the things in comments to the original post by downshift and neither of them worked.

How can I make the PyDev editor selectively ignore errors?

I'm using PyDev under Eclipse to write some Jython code. I've got numerous instances where I need to do something like this:
import com.work.project.component.client.Interface.ISubInterface as ISubInterface
The problem is that PyDev will always flag this as an error and say "Unresolved import: ISubInterface". The code works just fine, it's just that I'd rather not have these little white/red X-marks next to my code and have my Problems tab littered with these errors.
Is there a way I can add a magic comment or something like that to the end of the line to make PyDev ignore the false error, similar to how you can sprinkle comments like "# pylint: disable-msg=E1101" to make PyLint ignore errors?
Also, there's a possibility I'm just doing it wrong when it comes to using Java interfaces in Jython. In which case a little bit of guidance would be very much appreciated.
You can add a comment
##UnresolvedImport
##UnusedVariable
So your import becomes:
import com.work.project.component.client.Interface.ISubInterface as ISubInterface ##UnresolvedImport
That should remove the error/warning. There are other comments you can add as well.
Add the hash character # at the end of the line then with the cursor on the flagged error, press Ctrl-1. One of the options in the menu will be something like #UndefinedVariable. Adding this comment will cause PyDev to ignore the error.
You can make the ignore like the other posts suggest, but the real problem is that Pydev cannot find that class... If you add a .jar that contains that class to your PYTHONPATH it should be able to resolve it (or if you have a Java project that has that class, you should be able to mark that project as a Pydev project and add its bin folder to the project PYTHONPATH -- in which case that class should be found too).
It is not a PYTHONPATH issue. It is related to importing/using static class-internal members of a Java class. I am getting the same sort of thing all over the place e.g. when trying to use constants in java.awt.Color:
import java.awt.Color as Color
borderColor = Color.BLACK # get "Undefined variable from import: BLACK" error
There is no way I've found to import Color.BLACK in this case. Thanks to iceman for at least pointing out the ##UndefinedVariable flag. That helps a lot. Note also that this is NOT a jython problem, the code runs just fine. It's just an issue with PyDev.

Categories

Resources