☰Read-only archive⌕
Looking for help with Bokeh?

This is a read-only archive. Ask questions and join the conversation on GitHub.

Visit Community Support on GitHub →
Archived discussions · Captured October 28, 2026

import local modules in bokeh 0.11 using "bokeh serve"

Community Support
mr.peter.baek
mr.peter.baek

How can i import functions into main.py from local modules when using the bokeh serve command in bokeh 0.11?

If i take e.g. the stocks example, add a new function to the main.py which prints something to screen, everything works fine. If i move the function to a seperate file (module) and import it into main.py it comes with an importerror. I am running the bokeh serve command from the parent directory of the main.py. I have tried both python 2 and 3.

Is anybody else seeing this issue?

/Peter.

⌁   Link to post
Bryan
Bryan

Hi Peter,

This is discussed in the documentation:

  http://bokeh.pydata.org/en/latest/docs/user_guide/server.html#directory-format

I hope that we can find a way around the technical issues stand in the way of providing this capability, but it may turn out the "normal" local imports (e.g., "from .foo import bar") are simply not technically feasible, given the way Python's import machinery works. There are some other methods that might be helpful as (slighly clunky) workarounds described here (noting that we do fix up __file__ so that it is correct):

  python - How can I import a module dynamically given the full path? - Stack Overflow

Thanks,

Bryan

···

On Jan 25, 2016, at 5:04 AM, mr.peter.baek@gmail.com wrote:

How can i import functions into main.py from local modules when using the bokeh serve command in bokeh 0.11?

If i take e.g. the stocks example, add a new function to the main.py which prints something to screen, everything works fine. If i move the function to a seperate file (module) and import it into main.py it comes with an importerror. I am running the bokeh serve command from the parent directory of the main.py. I have tried both python 2 and 3.

Is anybody else seeing this issue?

/Peter.

--
You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bokeh+unsubscribe@continuum.io.
To post to this group, send email to bokeh@continuum.io.
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/59e0d555-0ccf-4507-bd83-29fc98d95437%40continuum.io\.
For more options, visit https://groups.google.com/a/continuum.io/d/optout\.

⌁   Link to post
wcopeland
wcopeland

Another solution that is a little less cumbersome is to just extend the system path so that it includes the directory containing your local module.
Example:

import sys
sys.path.extend([ ])

···

On Monday, January 25, 2016 at 6:03:40 AM UTC-8, Bryan Van de ven wrote:

Hi Peter,

This is discussed in the documentation:

    [http://bokeh.pydata.org/en/latest/docs/user_guide/server.html#directory-format](http://bokeh.pydata.org/en/latest/docs/user_guide/server.html#directory-format)

I hope that we can find a way around the technical issues stand in the way of providing this capability, but it may turn out the “normal” local imports (e.g., “from .foo import bar”) are simply not technically feasible, given the way Python’s import machinery works. There are some other methods that might be helpful as (slighly clunky) workarounds described here (noting that we do fix up file so that it is correct):

    [http://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path](http://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path)

Thanks,

Bryan

On Jan 25, 2016, at 5:04 AM, mr.pet...@gmail.com wrote:

How can i import functions into main.py from local modules when using the bokeh serve command in bokeh 0.11?

If i take e.g. the stocks example, add a new function to the main.py which prints something to screen, everything works fine. If i move the function to a seperate file (module) and import it into main.py it comes with an importerror. I am running the bokeh serve command from the parent directory of the main.py. I have tried both python 2 and 3.

Is anybody else seeing this issue?

/Peter.

–
You received this message because you are subscribed to the Google Groups “Bokeh Discussion - Public” group.

To unsubscribe from this group and stop receiving emails from it, send an email to bokeh+un...@continuum.io.

To post to this group, send email to bo...@continuum.io.

To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/59e0d555-0ccf-4507-bd83-29fc98d95437%40continuum.io.

For more options, visit https://groups.google.com/a/continuum.io/d/optout.

⌁   Link to post
dustin
dustin

I got this to work after some wrangling.

I have a directory structure like this:

myprogram

–some-code/
–myapp/
– main.py

``

I want to keep my some-code separate from the bokeh application (myapp).

When I was was working with a single module Bokeh application:

I used included this statement in main.py:

import sys; sys.path.insert(0, ‘…’)

``

And I ran main.py from inside the myapp directory:

bokeh serve main.py

``

Once I switched to a Bokeh app with a directory structure:

I had to use this import statement:

import sys
import os
sys.path.insert(0, os.path.abspath(‘.’))

``

And run the program from the myprogram directory:

bokeh–serve myapp

``

···

On Thursday, January 28, 2016 at 12:28:44 PM UTC-7, wcopeland wrote:

Another solution that is a little less cumbersome is to just extend the system path so that it includes the directory containing your local module.
Example:

import sys
sys.path.extend([ ])

On Monday, January 25, 2016 at 6:03:40 AM UTC-8, Bryan Van de ven wrote:

Hi Peter,

This is discussed in the documentation:

    [http://bokeh.pydata.org/en/latest/docs/user_guide/server.html#directory-format](http://bokeh.pydata.org/en/latest/docs/user_guide/server.html#directory-format)

I hope that we can find a way around the technical issues stand in the way of providing this capability, but it may turn out the “normal” local imports (e.g., “from .foo import bar”) are simply not technically feasible, given the way Python’s import machinery works. There are some other methods that might be helpful as (slighly clunky) workarounds described here (noting that we do fix up file so that it is correct):

    [http://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path](http://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path)

Thanks,

Bryan

On Jan 25, 2016, at 5:04 AM, mr.pet...@gmail.com wrote:

How can i import functions into main.py from local modules when using the bokeh serve command in bokeh 0.11?

If i take e.g. the stocks example, add a new function to the main.py which prints something to screen, everything works fine. If i move the function to a seperate file (module) and import it into main.py it comes with an importerror. I am running the bokeh serve command from the parent directory of the main.py. I have tried both python 2 and 3.

Is anybody else seeing this issue?

/Peter.

–
You received this message because you are subscribed to the Google Groups “Bokeh Discussion - Public” group.

To unsubscribe from this group and stop receiving emails from it, send an email to bokeh+un...@continuum.io.

To post to this group, send email to bo...@continuum.io.

To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/59e0d555-0ccf-4507-bd83-29fc98d95437%40continuum.io.

For more options, visit https://groups.google.com/a/continuum.io/d/optout.

⌁   Link to post

End of archived discussion · Replies are disabled