0

This is my directory structure

src\
  dirCode\
    dirSubCode\
      test.py
  Django\
    DjangoApp\
      Home\
       views.py

From within views.py I'm trying to import a class (let's call it Main) from test.py. The line in test.py is simply

from dirCode.dirSubCode.test import Main

and I'm getting a ModuleNotFoundError when I try to run the server. I printed out the os.sys.path and the second entry points to 'src\' while the first entry is simply ''. Since this is not the only app from where I'm going to need to call "external" code I'm trying to avoid hard-coding

sys.path.insert(0,'path\to\src\dirCode\dirSubCode\')

if that would even work. I've tried looking through documentation, looking at other StackOverflow questions (Import module from subfolder), etc. but I'm at a loss as to how to do this. I've tried going into the src\Django\DjangoApp\Home directory and just running a python console and simply trying to import the class but it's giving me the same error.

One odd development that I'm running into is that when I try to run it from the Anaconda Prompt (yes, I'm running Windows) it doesn't work but when I run the file in PyCharm it does work. If that helps provide some insight as to what might be going on I'd appreciate the help.

Jeff
  • 309
  • 4
  • 20
  • You can't do a relative import from a module that's not inside the same package. It has to be an absolute import. However, your import *is* absolute, not relative. Learn about the search path (what it does, what it's default is, how to control it in PyCharm), and don't modify it at runtime (inside a script). Or just put everything in the same package if it really belongs in one. Also, are any of those packages? You don't mention any `__init__.py` files. – jpmc26 May 30 '18 at 22:56

2 Answers2

0

The proper way to do this is to introduce a shared package that you can install into a virtualenv for each webapp. Either from an actual release, or by using

python setup.py develop

Form within the shared package you essentially create a symlink in the venv that allows you to just import.

A more modern and simpler version is

pip install -e .

inside the library. See Python setup.py develop vs install

deets
  • 6,285
  • 29
  • 28
  • Although I'm not using setup.py develop, this lead me to find this link: https://stackoverflow.com/questions/19048732/python-setup-py-develop-vs-install which lead to the ultimate answer of using pip install -e path/to/package – Jeff May 31 '18 at 13:39
  • 1
    Neat! Didn’t know that existed, will use it myself. – deets May 31 '18 at 20:19
  • 1
    Augmented my answer so your comment is more visible. – deets May 31 '18 at 20:25
0

I believe you need to set the PYTHONPATH variable, which is used to set the default search path for modules.

https://docs.python.org/2/using/cmdline.html#envvar-PYTHONPATH

As per the python docs the syntax is like so...

https://docs.python.org/3.6/using/windows.html#excursus-setting-environment-variables

To temporarily set environment variables, open Command Prompt and use the set command:

C:\>set PATH=C:\Program Files\Python 3.6;%PATH%
C:\>set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib
C:\>python

Pycharm- in the Run/Debug configuration- sets it when you have it checked off (which is the default).

pycharm image

shmuels
  • 1,039
  • 1
  • 9
  • 22