PyDev unresolved import errors on intra-package modules when using Grammar 3.x - pydev

I think there is a bug with respect to how PyDev (version 4.6) recognizes intra-package imports when selecting Grammar 3.x for the project preferences. I have a project like this:
foobar
mypack
__init__.py
mod1.py
mod2.py
mod2.py simply says
from mod1 import fun1
mod1.py simply says
def fun1():
print("Hey we are in fun1 in mod1")
If the project Python project preferences are set to use Grammar 3.0-3.5, with a Python 3.4 interpreter, and I open up mod2.py the line from mod1 import fun1 is highlighted with an error Unresolved import: fun1. If I change the Python project preferences to use Grammar 2.7, close the file mod2.py and reopen it, the error disappears. Just by changing the grammar back and forth, and closing/reopening the file, I can make the error appear/disappear.
So it seems that setting the Grammar to 3.x in PyDev causes intra-package imports to be incorrectly flagged as having an import error.
Any suggestions?

PyDev is doing right... on Python 3, relative imports must be written as:
from .mod1 import fun1
If the import doesn't start with a dot, it'll consider it an absolute import (and will properly show the error for you as with that absolute path the imported file cannot be resolved).

So my real issue was getting PyDev to not report errors about the imports, and to be able to debug modules buried in packages that have a main() in there for debugging. The solution for me was to use relative imports (as said in Fabio's answer), and then to do the following for debugging purposes. Let's say I want to run a module pack1.subpack2.subpack3.subpack4.modtodebug using PyDev, that has relative imports, and has a main() function. At the top level of my project, I have a module debugmain.py that reads
from pack1.subpack2.subpack3.subpack4.modtodebug import main as debugmain
if __name__ == '__main__':
debugmain()
I then have one run configuration for debugmain.py and each time I want to debug a different module, I just have to edit the code in debugmain.py to point to that module.
I hope this helps someone else with this kind of problem.

Related

Python importing from incorrect module (which bears the same name), VSC

I have two modules, both named connection.py in two separate environments listed below. Both of the folders containing connection.py are in my PYTHONPATH system environment variable.
However, if that of spec is not placed above that of bvbot, spec's test_connection.py attempts to import from the connection.py of bvbot.
If in cmd, I can resolve this by moving the path of spec above that of bvbot. But, in Visual Studio Code, spec's test_connection.py still imports from bvbot's connection.py.
The two environments of interest are:
C:\Users\You_A\Desktop\2016Coding\VirtualEnviroments\spec\spec_trading
C:\Users\You_A\Desktop\2016Coding\VirtualEnviroments\bvbot\Legacy_bvbot
Structure of the spec path above:
src/
spec_trading/
__init__.py
connection.py
tests/
__init__.py
connection.py
spec test_connection.py:
import pytest
from connection import Connection, OandaConnection
class TestConnection:
def test_poll_timeout(self):
connection = Connection()
timeout = 10.0
connection.set_poll_timeout(timeout)
assert connection.poll_timeout == timeout
What I am doing wrong here? How can I resolve this without resorting to manually faffing with my systems environment variables and resolve the VSC issue?
Easiest solution is to not use implicit relative imports (I assume this is Python 2.7). Basically use explicit relative imports and make sure the imports resolve within the package they are contained within instead of Python having to search sys.path for the module.
And if you are using Python 2.7, put from __future__ import absolute_import at the top of the file.

Why does the scala-ide not allow multiple package definitions at the top of a file?

In scala it is common practice to stack package statements to allow shorter imports, but when I load a file using stacked packages into the scala ide and I attempt to use an import starting with the same organization I get a compiler error from what appears to be the presentation compiler. The code compiles fine in sbt outside of the IDE.
An example code snippet is as follows:
package com.coltfred
package util
package time
import com.github.nscala_time.time.Imports._
On the import I get the error object github is not a member of package com.coltfred.util.com.
If I move the import to a single line the error will go away, but we've used this practice frequently in our code base so changing them all to be single line package statements would be a pain.
Why is this happening and is there anything I can do to fix it?
Edit:
I used the eclipse-sbt plugin to generate the eclipse project file for this. The directory structure is what it should be and all of the dependencies are in the classpath.
Edit 2:
It turns out there was a file in the test tree of the util package (which should have been in the same package), but had a duplicate package statement at the top. I didn't check the test tree because it shouldn't affect the compilation of the main tree, but apparently I was wrong.
Not sure why the Scala IDE is not liking this, but you can force the import to start at the top level using _root_:
import _root_.com.github.nscala_time.time.Imports._
See if that avoids irritating the IDE.
This is a common annoyance that annoyed paulp into an attempt to fix it. His idea was that a dir that doesn't contribute class files shouldn't be taken as a package. If you can take util as scala.util, you should do so in preference to foo.util where that util is empty.
The util dir is the usual suspect, because who doesn't have a util dir lying around, and in particular, ./util?
apm#mara:~/tmp/coltfred$ mkdir -p com/coltfred/util/time
apm#mara:~/tmp/coltfred$ mkdir -p com/coltfred/util/com
apm#mara:~/tmp/coltfred$ vi com/coltfred/util/time/test.scala
apm#mara:~/tmp/coltfred$ scalac com/coltfred/util/time/test.scala
./com/coltfred/util/time/test.scala:5: error: object github is not a member of package com.coltfred.util.com
import com.github.nscala_time.time._
^
one error found
apm#mara:~/tmp/coltfred$ cat com/coltfred/util/time/test.scala
package com.coltfred
package util
package time
import com.github.nscala_time.time._
class Test
apm#mara:~/tmp/coltfred$
To debug, find out where the offending package is getting loaded from.

Virtualenv PyDev Undefined variable from import error

First of all I'm aware of the question here but I couldn't find a satisfied answer there. I don't want to ignore errors or use comments - I want to have the right settings in eclipse/pydev. My problem is pretty similar to this one.
I'm using Ubuntu 12.04 and installed a virtuenv for python 2.7 in my home directory. After installing several python packages (numpy, scipy, matplotlib, etc.) using pip, I installed eclipse 4.3 with pydev.
If I use the python system interpreter at /usr/bin/python everything works fine (except that I didn't want to use). However, if I try to set up a python interpreter using the virtualenv first I get this warning describe here. After clicking "proceed anyway", it seems to work. So far so good.
However e.g. import numpy as np gives for each np.* call the eclipse/pydev error Undefined variable from import, also the code completion doesn't work properly. It seems to work, e.g. for datetime, but not for numpy, scipy and matplotlib.
Does anybody figured out to configure eclipse correctly?
I already tried to add the numpy path manually to the virtualenv interpreter, but than I get the weird error:
import matplotlib.dates as mpl_dates
File "/home/pydev/myenv-py27/local/lib/python2.7/site-packages/matplotlib/init.py", line 149, in
import sys, os, tempfile
File "/usr/lib/python2.7/tempfile.py", line 34, in
from random import Random as _Random
ImportError: cannot import name Random

Unresolved import errors yet successful import still occurs

I am working on a project in Eclipse Juno. I wrote a class called Character in a package named chargen.py. There's a red X next to from chargen import Character:
Unresolved import: Character
Character Found at: Avarice_v0.PlayAvarice_v0
from chargen import Character
Yet the import works. The entire code at the moment is simply this:
from chargen import Character
def main():
PLAYER = Character("")
print(PLAYER)
if __name__ == '__main__':
main()
This code results in the printing of the __str__ proving the Character("") ran. Also, it generates no errors when running. Why does Eclipse label this Unresolved import: Character?
I figured out how to get rid of the error. I moved everything one directory up. Settings for the project showed the PYTHONPATH included the main directory, but not the nested one. By moving it all up and deleting the now empty original folder, I have no unresolved import errors. This helped me to understand more about the PYTHONPATH choices offered at the initial setup of the project in PyDev.
In the properties for your project, there's a pane called "PyDev - PYTHONPATH", with a sub-pane called "External Libraries". You can add source folders (any folder that has an init.py) to the path using that pane. Your project code will then be able to import modules from those source folders.

"import as" leads to unresolved import error, "from .. import" does not

I'm trying to write some plugins for the irc bot supybot with eclipse/pydev. Pydev gives me errors about unresolved imports on supybot-modules/packages (e. g. import supybot.utils as utils), but works ok on e. g. "from supybot.commands import *". So I guess I set up dydev correctly, as it finds the wanted modules. The problem must be in pydev/eclipse, as the bot works correct and in eric5 I get also no errors about that.
Removing the interpreter and setting it up didn't help. Any other ideas on how to fix this? System: Arch Linux, Eclipse Juno, PyDev 2.7.1, wanted (and set up) python interpreter is 2.7, supybot is installed in site-packages for Python 2.7.
Edit: Just noticed: PyDev doesn't mark the "from ... import *" as error, but if I use functions imported from there I get an error on that function.
Code sample:
[...]
import supybot.utils as utils
from supybot.commands import *
[...]
wunsch = wrap(wunsch, ['text', 'now'])
[...]
Error on the first line: Unresolved import: utils
Second line gets no error nor warning
Error on 3rd line: Undefined variable: wrap
But 'wrap' is a function declared in supybot.commands
Run import supybot; print supybot.__path__ to get the path to the supybot package. PyDev may be importing the wrong one (for example if you use a folder called supybot in your workspace).