1

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?

Dave
  • 424
  • 3
  • 14
  • Take a look at `import sys; print(sys.path)`, the order of this should give you an idea. https://leemendelowitz.github.io/blog/how-does-python-find-packages.html – Joe Sep 16 '19 at 18:00
  • @Joe first thing I notice is that C:\Users\You_A\Desktop\2016Coding\VirtualEnviroments\spec\spec_trading\src\spec_trading is missing from sys.path when run via VSC cmd terminal. I'm lost, but I'll read up – Dave Sep 16 '19 at 18:49
  • You could try to add it manually to see if this fixes your problems https://stackoverflow.com/questions/16114391/adding-directory-to-sys-path-pythonpath – Joe Sep 16 '19 at 20:26
  • And if you run it from a "normal" terminal, not the one inside vscode? You are using virtual environments, right? – Joe Sep 16 '19 at 20:28
  • @joe normal terminal is fine. Using virtual environments. – Dave Sep 16 '19 at 21:18
  • Have you checked the VSC terminal which commands are executed on startup? Have you checked which Python interpreter is assigned to the project / folder? – Joe Sep 17 '19 at 06:58
  • C:\Users\You_A\Desktop\2016Coding\VirtualEnviroments\spec\spec_trading\src\spec_trading does not appear in sys.path or PYTHONPATH when run through VSC cmd.It does via regular cmd. I'm not sure what you are referring to re 'commands on startup. – Dave Sep 17 '19 at 07:26
  • python interpreter is correct. – Dave Sep 17 '19 at 07:31
  • Please check `sys.executable` for both cases, this will tell you the interpreter used. – Joe Sep 17 '19 at 07:56
  • At the top of the terminal in VSC there might be some commands showing VSC changing the virtual environments. – Joe Sep 17 '19 at 07:57
  • both interpreters used are the same ( each checked with sys.executable). VSC shows correct VE being used – Dave Sep 17 '19 at 08:15
  • 1
    Hm, strange. I am not sure if you are using the virtual environments in a correct way. Have you understood how they work? Usually there is only one environment active. It might be that this is what VSC is trying to do, it activates the one it thinks you need and puts it in the first place. You could manually override this by tinkering with `sys.path` before importing any modules. – Joe Sep 17 '19 at 08:38
  • Another option is to change the level you are importing from up one folder and use something like `from spec_trading import connection as spec_conn; from bvbot import connection as bv_conn`. – Joe Sep 17 '19 at 08:40
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/199558/discussion-between-joe-and-dave). – Joe Sep 17 '19 at 08:42

1 Answers1

1

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.

Brett Cannon
  • 14,438
  • 3
  • 45
  • 40