2

NOTE: this is a duplicate of import from local directory on python2.

Let me say upfront, sorry as I've read thru the import rules for python, ipython and jupython and can't keep them straight anymore...

Environment:

ubuntu linux server with jupyterhub
jupyter home is $HOME/opt
$HOME/opt/mynotebooks                      # contains my .pynb notebooks
$HOME/opt/mynotebooks/py_lib               # contains .py files
$HOME/opt/mynotebooks/py_lib/app_config.py # myapp config/links to myapp
$HOME/opt/mynotebooks/py_lib/class1.py     # contains .py files
/opt/myapp/src/app                         # contains my app .py classes
/opt/myapp/src/app/appclass.py             # contains appclass

My notebook1.pynb contains the following start-up cell :

import os, sys
MY_NOTEBOOK = os.path.join(os.getenv('HOME'),'opt','mynotebooks')
# add my notebook to ipython path
os.chdir(MY_NOTEBOOK)
sys.path.append(MY_NOTEBOOK) 
import py_lib.app_config
print(os.getenv('MYAPP_STORE')

The above cell works ok.

Next, the load py_lib.class1 cell to do work...

from py_lib.class1 import myclass1

But the above gives error: "No module named class1".

If I move the app_config.py into my notebook dir it works use the following start-up cell...(NOTE import app_config changed without py_lib... I restart kernel and clear all output before re-running). The sys.path is the same when I do the py_lib.class1 import. Why can't it find it when app_config is 1 level down? Makes no sense?

import os, sys
MY_NOTEBOOK = os.path.join(os.getenv('HOME'),'opt','mynotebooks')
# add my notebook to ipython path
os.chdir(MY_NOTEBOOK)
sys.path.append(MY_NOTEBOOK) 
import app_config
print(os.getenv('MYAPP_STORE')

(BTW: this latter startup fails if I save notebook to py and run via python.)

$HOME/opt/mynotebooks/nb_lib/app_config.py

APP_SITE = os.path.join('/opt/myapp/src')
APP_STORE = os.path.join('/opt/myapp/store')
os.environ['APP_SITE'] = APP_SITE
os.environ['APP_STORE'] = APP_STORE
# Link to APP_SITE
os.chdir(APP_SITE)
sys.path.append(APP_SITE)  
APP_UPLOAD = os.path.join(APP_STORE,'upload')
Community
  • 1
  • 1
frankr6591
  • 1,211
  • 1
  • 8
  • 14
  • IPython shouldn't make any difference to how imports work. Does it work if you run a regular Python script sitting in `~/opt/mynotebooks/`? Also, your description says `.../class1.py # contains .py files` - does that mean you have a directory called `class1.py`? Or is that just copy-pasted? – Thomas K Mar 07 '16 at 22:59
  • Been reading... (forgot where) there seems to be a difference between IPython/python on import... at least I have observed it. The TOP/py_lib is a packagfe (has a __init__.py) and one of the files is class1.py containing 'class myclass(object):....' – frankr6591 Mar 08 '16 at 06:08
  • I did save the notebook as a .py file... and when I try to run the .py file with imports... the 'import app_config doesn't work. Thus I moved it into the py_lib and 'import py_lib.app_config' works... but now the notebook didn't work??? Thus my query... – frankr6591 Mar 08 '16 at 06:09
  • I can't work out what problem relates to what description - you appear to have tried a lot of different things, and it's not clear what order they are in or what the situation is now. I'd suggest you start with a clean directory and build the pieces up from scratch, without using `os.chdir` or `sys.path.append`, to get a simplified example of the problem. You might even find that when you do that you can't reproduce the problem. – Thomas K Mar 08 '16 at 12:03
  • Tks @Thomas... your comment inspired me to build a sample which demostrates the issue and what the objective is. You can look in [AppNotebooks example](https://github.com/frankr6591/AppNotebooks) if you want to see the code. I found the issue to be py2 vs py3 handling of current directory. Thanks again. – frankr6591 Mar 08 '16 at 16:51
  • While I think it's generally a good idea to use Python 3, I don't think that's the real issue here. You're still using `os.chdir()` and `sys.path.append()` quite a bit, you're changing global state when modules are imported, you're hardcoding paths, you're committing the generated `.pyc` files... it's hard to know where to start unpicking it, but I think I'd say ditch the `os.chdir()` calls first. It doesn't look like there's any need for them. – Thomas K Mar 08 '16 at 23:33

1 Answers1

1

I found the issue to be a duplicate of import from local directory.

Specifically, "Python 2.5 for Ubuntu 8.10 does not have the current directory (empty string) in sys.path for the interpreter."

Unfortunately, my app is still on py2 so I run the notebook on py2. I have put a sample of the AppNotebooks. So fix is to migrate to py3. Or for app, put config into a sub package (ie. py_lib).

Community
  • 1
  • 1
frankr6591
  • 1,211
  • 1
  • 8
  • 14