Select to view content in your preferred language

Pro: Access notebook env in python window

203
4
a month ago
chris_del101
Occasional Contributor

In Pro how can I access the notebook values inside the python window command line? The equivalent in a normal environment might be something like this. For me using python like this is very common.

 

>>> import src.portal_app as app
<module 'src.portal_app' from 'C:\\Users\\me\\code\\source\\repos\\arcgis-cloner\\src\\portal_app.py'>
>>> app.app_credentials
<module 'src.credentials.app_credentials' from 'C:\\Users\\me\\code\\source\\repos\\arcgis-cloner\\src\\credentials\\app_credentials.py'>

 

 In Pro I have a notebook with values.

 

my_var = 1
env = arcpy.env.workspace

 

How can I get these to the command line in Pro? How do I import the notebook? Might be super simple but just can't find the docs on how to do it.

 

my_var
Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'my_var' is not defined

If you suspect this is an IPython 8.18.1 bug, please report it at:
    https://github.com/ipython/ipython/issues
or send an email to the mailing list at ipython-dev@python.org

You can print a more detailed traceback right now with "%tb", or use "%debug"
to interactively debug it.

Extra-detailed tracebacks for bug-reporting purposes can be enabled via:
    %config Application.verbose_crash=True

 

 

0 Kudos
4 Replies
TonyAlmeida
MVP Regular Contributor

Have you tried exec()?

 

exec(open("your_script_location_path/notebook.py").read())

 

 

0 Kudos
HaydenWelch
MVP

That would work maybe for one file, but for an entire repository you'd need to add everything to the system path. Also using exec() is usually bad practice since it skips a lot of stuff handed by import so you could possibly run into conflicts or unexpected overrides if you're running it on a bunch of files with global variables that have the same name (which in my experience is common with arcpy scripts).

Luke_Pinner
MVP Regular Contributor

I didn't know this was possible, nor have I tested it as it's not something I would use (I'd just use a plain py file to hold that sort of thing and import that) but this might help even though it's not simple:

Importing Jupyter Notebooks as Modules

0 Kudos
HaydenWelch
MVP

The ArcPro command line exists in a gated conda environment. You can either create a new environment in the settings window that includes your module, or just run this to add it to the active interpreter session:

 

import sys
src=r"C:\Path\To\src"
sys.path.append(src)

 

This allows you to import anything from your src directory as if it was added to your system level python environment

HaydenWelch_1-1736878009706.png

This does only work if your notebooks are regular python files though, inter-connectivity between Notebooks and a standard interpreter is a bit more tricky (as Luke's reply points out). If you can somehow translate your python notebooks to modules that would be the easiest solution. Otherwise you can start with this, then create an "import_notebooks()" function that runs after you add the src repo to your interpreter path:

 

import sys
src=r"C:\Path\To\src"
sys.path.append(src)

>>> from notebook_importer import import_notebooks
>>> import_notebooks(src)

 

You'll have to implement the import_notebooks function yourself, as I'm not familiar with how to do that in the latest Jupyter release. The code shared by Luke is old, but doing it this way should make it easier. Especially if you have this run in the __init__.py file of your module so you don't even need to run the import function. You can just import notebooks and have that module handle the business logic of importing the notebooks and adding them to the system path

 

Also if you want to be really dirty, you can edit your local arcpy to include your module import so they're imported by default. I wouldn't recommend this for production as it would be better to modify your conda environment to include your repository, but for testing or development, just slapping that sys code at the top of the arcpy __init__ file would technically work and save you a lot of headache until you can get around to building a real environment.