1

I've a project in which a python script must get the path to some of its imports from an environment variable. I'm handling this with

sys.path.insert(0, os.environ["PYDIR"])

which works fine when I run the application.

Pylint (in my Vim editor) is flagging import errors on all the modules I import from "PYDIR". I hate to disable the error message in the module since it's usually useful. OTOH, I also hate the visual clutter in my editor.

I suspect there's no good solution since Pylint doesn't execute the code but thought I'd ask anyway.

EDIT: See my below comment on difference between this question and PyLint "Unable to import" error - how to set PYTHONPATH?

Community
  • 1
  • 1
Mike Ellis
  • 1,120
  • 1
  • 12
  • 27
  • Possible duplicate of [PyLint "Unable to import" error - how to set PYTHONPATH?](http://stackoverflow.com/questions/1899436/pylint-unable-to-import-error-how-to-set-pythonpath) – Zero Piraeus Feb 27 '17 at 19:22
  • @ZeroPiraeus I checked that one before posting. It's not the same despite the similarities. S/he asked about a static path. My problem involves a dynamic path that depends on an environment variable. – Mike Ellis Feb 27 '17 at 19:31
  • I'm not in a position to test this right now, but what's stopping you from using the same technique e.g. `init-hook='import os, sys; sys.path.insert(0, os.environ["PYDIR"])'`? – Zero Piraeus Feb 27 '17 at 19:58
  • You could add `$PYDIR` to the environment by modifying [`os.environ`](https://docs.python.org/2/library/os.html#os.environ) if necessary as well. – Zero Piraeus Feb 27 '17 at 20:00

1 Answers1

0

For my particular case, the following seems to work satisfactorily. (I'm developing on OS X, I start a customized Terminal shell when working on this project, and my editor is 'mvim' (MacVim))

Add to the Terminal startup shell command list:

source .myprojectshellenv;

where .myprojectshellenv contains:

export PYDIR=path-to-myproject-python-modules
alias pmvim="env PYTHONPATH=$PYDIR mvim"

Then all I need remember is to edit the files with pmvim instead of mvim. (If I forget the error indicators from pylint (via syntastic) make it immediately obvious)

Mike Ellis
  • 1,120
  • 1
  • 12
  • 27