Repository: holoviz/panel

Issue:
Panel tries to use unexpectedly Django, while executing in Jupyterlab 
#### ALL software version info
windows 10, pip install
<details>
Package                       Version
----------------------------- ------------
adodbapi                      2.6.1.3
affine                        2.3.1
aiofiles                      0.8.0
aiohttp                       3.8.1
aiosignal                     1.2.0
aiosqlite                     0.17.0
alabaster                     0.7.12
alembic                       1.7.7
algopy                        0.5.7
altair                        4.2.0
altair-data-server            0.4.1
altair-transform              0.2.0
altair-widgets                0.2.2
altgraph                      0.17.2
amply                         0.1.5
aniso8601                     9.0.1
ansiwrap                      0.8.4
anyio                         3.5.0
appdirs                       1.4.4
argon2-cffi                   21.3.0
argon2-cffi-bindings          21.2.0
arrow                         1.2.2
asciitree                     0.3.3
asgi-csrf                     0.9
asgiref                       3.5.0
asn1crypto                    1.4.0
asteval                       0.9.26
astor                         0.8.1
astroid                       2.11.3
astroML                       1.0.2.post1
astropy                       5.0.4
async-generator               1.10
async-timeout                 4.0.2
atomicwrites                  1.4.0
attrs                         21.4.0
autopep8                      1.6.0
Babel                         2.10.1
backcall                      0.2.0
backports-abc                 0.5
baresql                       0.7.6
base58                        2.1.1
bcrypt                        3.2.0
beautifulsoup4                4.11.1
binaryornot                   0.4.4
black                         22.3.0
bleach                        5.0.0
blinker                       1.4
blis                          0.7.7
blosc                         1.10.6
bokeh                         2.4.2
Bottleneck                    1.3.4
bqplot                        0.12.33
branca                        0.5.0
brewer2mpl                    1.4.1
Brotli                        1.0.9
cachelib                      0.6.0
cachetools                    5.0.0
Cartopy                       0.20.2
catalogue                     2.0.7
certifi                       2021.10.8
cffi                          1.15.0
cftime                        1.6.0
chardet                       4.0.0
charset-normalizer            2.0.12
click                         8.0.4
click-default-group           1.2.2
click-plugins                 1.1.1
cligj                         0.7.2
cloudpickle                   2.0.0
clr-loader                    0.1.7
clrmagic                      0.0.1a2
colorama                      0.4.4
colorcet                      3.0.0
commonmark                    0.9.1
cookiecutter                  1.7.3
coverage                      6.3.2
cramjam                       2.5.0
cryptography                  3.4.7
csvs-to-sqlite                1.3.1a0
cvxopt                        1.3.0
cvxpy                         1.2.0
cx-Freeze                     6.10
cx-Logging                    3.0
cycler                        0.11.0
cymem                         2.0.6
Cython                        0.29.28
cytoolz                       0.11.2
dash                          2.3.0
dash-core-components          2.0.0
dash-html-components          2.0.0
dash-table                    5.0.0
dask                          2022.4.2
dask-glm                      0.2.0
dask-image                    2021.12.0
dask-labextension             5.2.0
dask-ml                       2022.1.22
dask-searchcv                 0.2.0
databases                     0.5.5
datasette                     0.61.1
datasette-graphql             2.0.1
datashader                    0.14.0
datashape                     0.5.2
dateparser                    1.1.0
db.py                         0.5.4b1
deap                          1.3.1
debugpy                       1.6.0
decorator                     4.4.2
defusedxml                    0.7.1
Deprecated                    1.2.13
deprecation                   2.1.0
diff-match-patch              20200713
dill                          0.3.3
distlib                       0.3.4
distributed                   2022.4.2
Django                        4.0.4
docopt                        0.6.2
docrepr                       0.1.1
docutils                      0.17.1
ecos                          2.0.10
emcee                         3.1.1
entrypoints                   0.4
et-xmlfile                    1.1.0
fast-histogram                0.10
fastapi                       0.75.2
fasteners                     0.17.3
fastjsonschema                2.15.3
fastparquet                   0.8.0
feather-format                0.4.1
filelock                      3.6.0
Fiona                         1.8.21
flake8                        4.0.1
Flask                         2.1.2
flask-accepts                 0.18.4
Flask-Compress                1.12
Flask-Mail                    0.9.1
flask-restx                   0.5.1
Flask-Session                 0.4.0
Flask-SQLAlchemy              2.5.1
flaskerize                    0.14.0
flit                          3.7.1
flit_core                     3.7.1
folium                        0.12.1
fonttools                     4.31.2
formlayout                    1.2.1a1
frozenlist                    1.3.0
fs                            2.4.15
fsspec                        2022.3.0
future                        0.18.2
fuzzywuzzy                    0.18.0
GDAL                          3.4.2
geographiclib                 1.52
geopandas                     0.10.2
geopy                         2.2.0
geoviews                      1.9.5
gitdb                         4.0.9
GitPython                     3.1.26
gmpy2                         2.1.2
graphene                      3.0
graphql-core                  3.1.7
graphql-relay                 3.1.5
great-expectations            0.14.11
greenlet                      1.1.2
guidata                       2.1.1
guiqwt                        4.1.0
h11                           0.12.0
h2                            4.1.0
h5py                          3.6.0
HeapDict                      1.0.1
holoviews                     1.14.8
hpack                         4.0.0
html5lib                      1.1
httpcore                      0.14.7
httpie                        3.1.0
httptools                     0.4.0
httpx                         0.22.0
hupper                        1.10.3
husl                          4.0.3
hvplot                        0.7.3
hypercorn                     0.13.2
hyperframe                    6.0.1
hypothesis                    6.45.1
idlex                         1.22
idna                          3.1
imageio                       2.17.0
imageio-ffmpeg                0.4.2
imagesize                     1.3.0
imbalanced-learn              0.9.0
importlib-metadata            4.11.3
inflection                    0.5.1
iniconfig                     1.1.1
intake                        0.6.4
intervaltree                  3.0.2
ipycanvas                     0.11.0
ipykernel                     6.13.0
ipyleaflet                    0.14.0
ipympl                        0.9.1
ipython                       7.32.0
ipython-genutils              0.2.0
ipython-sql                   0.4.1b1
ipywidgets                    7.7.0
isort                         5.10.1
itsdangerous                  2.1.2
janus                         1.0.0
jedi                          0.18.1
jellyfish                     0.9.0
Jinja2                        3.0.3
jinja2-time                   0.2.0
joblib                        1.1.0
json5                         0.9.6
jsonpatch                     1.32
jsonpointer                   2.3
jsonschema                    4.4.0
julia                         0.5.7
jupyter                       1.0.0
jupyter-bokeh                 3.0.4
jupyter-client                7.3.0
jupyter-console               6.4.3
jupyter-core                  4.10.0
jupyter-lsp                   1.5.1
jupyter-packaging             0.12.0
jupyter-server                1.17.0
jupyter-server-mathjax        0.2.5
jupyter-server-proxy          3.2.1
jupyter-sphinx                0.3.2
jupyterlab                    3.3.4
jupyterlab-git                0.34.2
jupyterlab-launcher           0.13.1
jupyterlab-lsp                3.10.1
jupyterlab-pygments           0.2.2
jupyterlab-server             2.13.0
jupyterlab-widgets            1.1.0
keyring                       23.5.0
kiwisolver                    1.4.2
langcodes                     3.3.0
lazy-object-proxy             1.7.1
llvmlite                      0.38.0
lmfit                         1.0.3
locket                        1.0.0
loky                          3.1.0
lxml                          4.8.0
lz4                           4.0.0
Mako                          1.2.0
Markdown                      3.3.6
MarkupSafe                    2.1.1
marshmallow                   3.12.1
matplotlib                    3.5.1
matplotlib-inline             0.1.3
maturin                       0.12.14
mccabe                        0.6.1
mercantile                    1.2.1
mergedeep                     1.3.4
metakernel                    0.28.2
mistune                       0.8.4
mizani                        0.7.4
mkl-service                   2.4.0
mlxtend                       0.18.0
moviepy                       1.0.3
mpl-scatter-density           0.7
mpld3                         0.5.7
mpldatacursor                 0.7.1
mpmath                        1.2.1
msgpack                       1.0.3
msvc-runtime                  14.29.30133
multidict                     6.0.2
multipledispatch              0.6.0
munch                         2.5.0
murmurhash                    1.0.6
mypy                          0.942
mypy-extensions               0.4.3
mysql-connector-python        8.0.21
nbclassic                     0.3.7
nbclient                      0.5.13
nbconvert                     6.5.0
nbconvert_reportlab           0.2
nbdime                        3.1.1
nbformat                      5.3.0
nbval                         0.9.6
nest-asyncio                  1.5.5
netCDF4                       1.5.8
networkx                      2.8
NLopt                         2.7.0
nltk                          3.7
notebook                      6.4.11
notebook-shim                 0.1.0
numba                         0.55.1
numcodecs                     0.9.1
numdifftools                  0.9.40
numexpr                       2.8.1
numpy                         1.21.6
numpydoc                      1.2.1
oct2py                        5.4.3
octave-kernel                 0.34.1
openpyxl                      3.0.9
orjson                        3.6.7
osqp                          0.6.2.post4
outcome                       1.1.0
packaging                     21.3
palettable                    3.3.0
pandas                        1.4.2
pandas-datareader             0.10.0
pandocfilters                 1.5.0
panel                         0.13.0
papermill                     2.3.4
param                         1.12.1
parambokeh                    0.2.3
paramiko                      2.8.0
paramnb                       2.0.4
parso                         0.8.3
partd                         1.2.0
pathspec                      0.9.0
pathy                         0.6.1
patsy                         0.5.2
pdfrw                         0.4.post2
pdvega                        0.2.1.dev0
pefile                        2021.9.3
pep8                          1.7.1
pexpect                       4.8.0
pg8000                        1.23.0
pickleshare                   0.7.5
Pillow                        9.1.0
PIMS                          0.5
Pint                          0.19.2
pip                           22.0.4
pipdeptree                    2.2.1
pkginfo                       1.8.2
platformdirs                  2.5.2
plotly                        5.7.0
plotnine                      0.8.1.dev0
pluggy                        1.0.0
ply                           3.11
portpicker                    1.5.0
poyo                          0.5.0
ppci                          0.5.8
preshed                       3.0.6
prettytable                   3.2.0
priority                      2.0.0
proglog                       0.1.9
prometheus-client             0.14.1
prompt-toolkit                3.0.29
protobuf                      4.0.0rc1
psutil                        5.9.0
ptpython                      3.0.20
ptyprocess                    0.7.0
PuLP                          2.3
py                            1.11.0
py-lru-cache                  0.1.4
pyaml                         20.4.0
pyarrow                       7.0.0
PyAudio                       0.2.11
pybars3                       0.9.7
pybind11                      2.9.2
pycodestyle                   2.8.0
pycosat                       0.6.3
pycparser                     2.21
pyct                          0.4.8
pydantic                      1.8.2
pydeck                        0.7.1
pydocstyle                    6.1.1
pyepsg                        0.4.0
pyerfa                        2.0.0.1
pyflakes                      2.4.0
pyflux                        0.4.17
pygad                         2.16.3
pygame                        2.1.2
pygbm                         0.1.0
Pygments                      2.11.2
pyhdf                         0.10.3
pyinstaller                   5.0.1
pyinstaller-hooks-contrib     2022.4
pylint                        2.13.7
pyls-spyder                   0.4.0
pymc                          2.3.8
PyMeta3                       0.5.1
pymongo                       4.1.1
Pympler                       1.0.1
PyNaCl                        1.5.0
pynndescent                   0.5.6
pyodbc                        4.0.32
PyOpenGL                      3.1.6
pypandoc                      1.5
pyparsing                     2.4.7
pyproj                        3.3.1
PyQt5                         5.15.6
PyQt5-Qt5                     5.15.2
PyQt5-sip                     12.9.1
pyqtgraph                     0.12.4
PyQtWebEngine                 5.15.5
PyQtWebEngine-Qt5             5.15.2
pyrsistent                    0.18.1
pyserial                      3.5
pyshp                         2.2.0
PySocks                       1.7.1
pystache                      0.5.4
pytest                        7.1.1
python-baseconv               1.2.2
python-dateutil               2.8.2
python-dotenv                 0.19.2
python-hdf4                   0.10.0+dummy
python-Levenshtein            0.12.2
python-lsp-black              1.2.1
python-lsp-jsonrpc            1.0.0
python-lsp-server             1.4.1
python-multipart              0.0.5
python-picard                 0.7
python-slugify                6.1.2
python-snappy                 0.6.1
pythonnet                     3.0.0a2
PythonQwt                     0.10.1
pytz                          2022.1
pytz-deprecation-shim         0.1.0.post0
pyviz-comms                   2.2.0
PyWavelets                    1.3.0
pywin32                       303
pywin32-ctypes                0.2.0
pywinpty                      2.0.5
pywinusb                      0.4.2
PyYAML                        6.0
pyzmq                         22.3.0
pyzo                          4.12.3
pyzstd                        0.15.2
QDarkStyle                    3.0.3
qdldl                         0.1.5.post2
qpsolvers                     1.8.0
qstylizer                     0.2.1
QtAwesome                     1.1.1
qtconsole                     5.3.0
QtPy                          2.1.0
quadprog                      0.1.8
quantecon                     0.5.3
quart                         0.17.0
rasterio                      1.2.10
readme-renderer               35.0
redis                         4.2.2
regex                         2022.3.15
reportlab                     3.6.9
requests                      2.27.1
requests-toolbelt             0.9.1
rfc3986                       1.5.0
rich                          12.3.0
rise                          5.7.1
rope                          1.0.0
Rtree                         1.0.0
ruamel.yaml                   0.17.17
Rx                            3.1.1
scikit-fuzzy                  0.4.1
scikit-image                  0.19.2
scikit-learn                  1.0.2
scikit-optimize               0.9.0
scilab2py                     0.6.2
scipy                         1.8.0
scramp                        1.4.1
scs                           3.2.0
seaborn                       0.11.2
Send2Trash                    1.8.0
setuptools                    62.1.0
shap                          0.40.0
Shapely                       1.8.1.post1
simpervisor                   0.4
simplegeneric                 0.8.1
simplejson                    3.17.6
simpy                         4.0.1
six                           1.16.0
sklearn-contrib-lightning     0.6.2.post0
slicer                        0.0.7
slicerator                    1.1.0
smart-open                    5.2.1
smmap                         5.0.0
snakeviz                      2.1.0
sniffio                       1.2.0
snowballstemmer               2.2.0
snuggs                        1.4.7
sortedcontainers              2.4.0
sounddevice                   0.4.4
soupsieve                     2.3.2.post1
spacy                         3.2.4
spacy-legacy                  3.0.9
spacy-loggers                 1.0.2
Sphinx                        4.5.0
sphinx-rtd-theme              1.0.0
sphinxcontrib-applehelp       1.0.2
sphinxcontrib-devhelp         1.0.2
sphinxcontrib-htmlhelp        2.0.0
sphinxcontrib-jsmath          1.0.1
sphinxcontrib-qthelp          1.0.3
sphinxcontrib-serializinghtml 1.1.5
spyder                        5.3.0
spyder-kernels                2.3.0
SQLAlchemy                    1.4.35
sqlite-bro                    0.12.2
sqlite-fts4                   1.0.1
sqlite-utils                  3.26
sqlparse                      0.4.2
srsly                         2.4.2
starlette                     0.17.1
statsmodels                   0.13.2
streamlit                     1.4.0
streamz                       0.6.3
supersmoother                 0.4
swifter                       1.0.9
sympy                         1.10.1
tables                        3.7.0
tabulate                      0.8.9
tblib                         1.7.0
tenacity                      8.0.1
termcolor                     1.1.0
terminado                     0.13.3
text-unidecode                1.3
textdistance                  4.2.2
textwrap3                     0.9.2
thinc                         8.0.15
threadpoolctl                 3.1.0
three-merge                   0.1.1
tifffile                      2022.4.22
tinycss2                      1.1.1
toml                          0.10.2
tomli                         2.0.1
tomli_w                       1.0.0
tomlkit                       0.10.2
toolz                         0.11.2
tornado                       6.1
tqdm                          4.64.0
traitlets                     5.1.1
traittypes                    0.2.1
trio                          0.20.0
trio-asyncio                  0.12.0
twine                         4.0.0
twitter                       1.19.2
typer                         0.4.1
typing_extensions             4.2.0
tzdata                        2022.1
tzlocal                       4.2
ujson                         5.2.0
umap-learn                    0.5.1
uncertainties                 3.1.6
urllib3                       1.26.9
uvicorn                       0.17.6
validators                    0.18.2
vega                          3.6.0
vega-datasets                 0.9.0
virtualenv                    20.14.1
ViTables                      3.0.2
voila                         0.3.5
wasabi                        0.9.1
wasmer                        1.1.0
wasmer_compiler_cranelift     1.1.0
wasmer_compiler_singlepass    1.1.0
watchdog                      2.1.7
watchgod                      0.8.2
wcwidth                       0.2.5
webencodings                  0.5.1
websocket-client              1.3.2
websockets                    10.3
Werkzeug                      2.1.2
wheel                         0.37.1
widgetsnbextension            3.6.0
winpython                     4.6.20220501
wordcloud                     1.8.1
wrapt                         1.14.0
wsproto                       1.1.0
xarray                        2022.3.0
XlsxWriter                    3.0.3
xlwings                       0.24.7
yapf                          0.32.0
yarl                          1.7.2
zarr                          2.11.3
zict                          2.2.0
zipp                          3.8.0
zstandard                     0.17.0
</details>

#### Description of expected behavior and the observed behavior
Adding Django to my list of packages, suddenly Holoviews/Panel doesn't behave the same in Jupyterlab: it wants to import (and use ?) use Django.

It shall just use Jupyterlab

Did I do something wrong/uncomplete ?

#### Complete, minimal, self-contained example code that reproduces the issue

under jupyterlab

````
# temporary warning removal
import warnings
import matplotlib as mpl
warnings.filterwarnings("ignore", category=mpl.cbook.MatplotlibDeprecationWarning)
# Holoviews
# for more example, see http://holoviews.org/Tutorials/index.html
import numpy as np
import holoviews as hv
hv.extension('bokeh')
dots = np.linspace(-0.45, 0.45, 11)
fractal = hv.Image(image)

layouts = {y: (fractal * hv.Points(fractal.sample([(i,y) for i in dots])) +
               fractal.sample(y=y) )
            for y in np.linspace(0, 0.45,11)}

hv.HoloMap(layouts, kdims=['Y']).collate().cols(2)`
````



#### Stack traceback and/or browser JavaScript console output


````
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_1348\1168282363.py in <cell line: 8>()
      6 # for more example, see http://holoviews.org/Tutorials/index.html
      7 import numpy as np
----> 8 import holoviews as hv
      9 hv.extension('bokeh')
     10 dots = np.linspace(-0.45, 0.45, 11)

C:\WinP\bd310\bublas\WPy64-31040\python-3.10.4.amd64\lib\site-packages\holoviews\__init__.py in <module>
     10 
     11 from . import util                                       # noqa (API import)
---> 12 from .annotators import annotate                         # noqa (API import)
     13 from .core import archive, config                        # noqa (API import)
     14 from .core.boundingregion import BoundingBox             # noqa (API import)

C:\WinP\bd310\bublas\WPy64-31040\python-3.10.4.amd64\lib\site-packages\holoviews\annotators.py in <module>
      8 import param
      9 
---> 10 from panel.pane import PaneBase
     11 from panel.layout import Row, Tabs
     12 from panel.util import param_name

C:\WinP\bd310\bublas\WPy64-31040\python-3.10.4.amd64\lib\site-packages\panel\__init__.py in <module>
     45 https://panel.holoviz.org/getting_started/index.html
     46 """
---> 47 from . import layout # noqa
     48 from . import links # noqa
     49 from . import pane # noqa

C:\WinP\bd310\bublas\WPy64-31040\python-3.10.4.amd64\lib\site-packages\panel\layout\__init__.py in <module>
----> 1 from .accordion import Accordion # noqa
      2 from .base import Column, ListLike, ListPanel, Panel, Row, WidgetBox # noqa
      3 from .card import Card # noqa
      4 from .flex import FlexBox # noqa
      5 from .grid import GridBox, GridSpec # noqa

C:\WinP\bd310\bublas\WPy64-31040\python-3.10.4.amd64\lib\site-packages\panel\layout\accordion.py in <module>
      3 from bokeh.models import Column as BkColumn, CustomJS
      4 
----> 5 from .base import NamedListPanel
      6 from .card import Card
      7 

C:\WinP\bd310\bublas\WPy64-31040\python-3.10.4.amd64\lib\site-packages\panel\layout\base.py in <module>
     11 from ..io.model import hold
     12 from ..io.state import state
---> 13 from ..reactive import Reactive
     14 from ..util import param_name, param_reprs
     15 

C:\WinP\bd310\bublas\WPy64-31040\python-3.10.4.amd64\lib\site-packages\panel\reactive.py in <module>
     31 )
     32 from .util import edit_readonly, escape, updating
---> 33 from .viewable import Layoutable, Renderable, Viewable
     34 
     35 log = logging.getLogger('panel.reactive')

C:\WinP\bd310\bublas\WPy64-31040\python-3.10.4.amd64\lib\site-packages\panel\viewable.py in <module>
     24 
     25 from .config import config, panel_extension
---> 26 from .io import serve
     27 from .io.document import init_doc
     28 from .io.embed import embed_state

C:\WinP\bd310\bublas\WPy64-31040\python-3.10.4.amd64\lib\site-packages\panel\io\__init__.py in <module>
     23     from .server import serve # noqa
     24     if 'django' in sys.modules:
---> 25         from . import django # noqa

C:\WinP\bd310\bublas\WPy64-31040\python-3.10.4.amd64\lib\site-packages\panel\io\django.py in <module>
      2 from urllib.parse import urlparse, urljoin
      3 
----> 4 from bokeh.server.django.consumers import DocConsumer, AutoloadJsConsumer
      5 
      6 from ..util import edit_readonly

C:\WinP\bd310\bublas\WPy64-31040\python-3.10.4.amd64\lib\site-packages\bokeh\server\django\__init__.py in <module>
      3 
      4 # Bokeh imports
----> 5 from .apps import DjangoBokehConfig
      6 from .routing import autoload, directory, document
      7 from .static import static_extensions

C:\WinP\bd310\bublas\WPy64-31040\python-3.10.4.amd64\lib\site-packages\bokeh\server\django\apps.py in <module>
     27 
     28 # Bokeh imports
---> 29 from .routing import Routing, RoutingConfiguration
     30 
     31 #-----------------------------------------------------------------------------

C:\WinP\bd310\bublas\WPy64-31040\python-3.10.4.amd64\lib\site-packages\bokeh\server\django\routing.py in <module>
     25 # External imports
     26 from channels.http import AsgiHandler
---> 27 from django.conf.urls import url
     28 from django.urls.resolvers import URLPattern
     29 

ImportError: cannot import name 'url' from 'django.conf.urls' (C:\WinP\bd310\bublas\WPy64-31040\python-3.10.4.amd64\lib\site-packages\django\conf\urls\__init__.py)
````

#### Screenshots or screencasts of the bug in action

![image](https://user-images.githubusercontent.com/4312421/166456633-3b0beaae-3c9d-4af3-9d21-58e6208e8061.png)



Discussion:
it creates a nightmar to me, of packages addition, then of Bokeh that seems to ignore some change with Django-4

````
site-packages\bokeh\server\django\routing.py in <module>
     25 # External imports
     26 from channels.http import AsgiHandler
---> 27 from django.conf.urls import url
````

see https://stackoverflow.com/a/70319607
Noticed the same problem today. I think the culprit is these lines:

``` python-traceback
C:\WinP\bd310\bublas\WPy64-31040\python-3.10.4.amd64\lib\site-packages\panel\io\__init__.py in <module>
     23     from .server import serve # noqa
     24     if 'django' in sys.modules:
---> 25         from . import django # noqa
```

I think the import is needed to be wrapped with a try/except.  
downgrading to Django-3.2.13 avoids the bug to snowball into full breakage for me, but it's a wired workaround to the wired problem.
+1 on wrapping with a try/except

Resolving patch:
diff --git a/panel/io/__init__.py b/panel/io/__init__.py
--- a/panel/io/__init__.py
+++ b/panel/io/__init__.py
@@ -22,7 +22,10 @@
 else:
     from .server import serve # noqa
     if 'django' in sys.modules:
-        from . import django # noqa
+        try:
+            from . import django # noqa
+        except ImportError:
+            pass
 
 __all__ = (
     "PeriodicCallback",


Test patch: