简体   繁体   中英

jupyter notebook: NameError: name 'c' is not defined

Every time that i try to launch my notebook im getting the error below . let's specify that im new worker on the project and the file config.py was created before that i joined the team.

Does anyone knows how to resolve it please?

The code actually done is

Requirements.txt

psycopg2==2.7.3.2.  
SQLAlchemy==1.2.2
pandas==0.21.0
docker==3.3.0
python-json-logger
sshtunnel==0.1.4
jupyter
jupytext==1.2
geopy==2.2.0

errror detail

~/SG/notebooks/config.py in <module>
1 # Using jupytext
----> 2 c.NotebookApp.contents_manager_class = "jupytext.TextFileContentsManager"
3 c.ContentsManager.default_jupytext_formats = "ipynb,py"

NameError: name 'c' is not defined

code

the row causing the error in the notebook is

  from src.util.connect_postgres import postgres_connexion

the content of the file connect_postgres

 from sqlalchemy import create_engine
 from config.util.database import TARGET_TEST_HOST, TARGET_PROD_HOST, \
 TARGET_TEST_DB, TARGET_PROD_DB, TARGET_TEST_USER, TARGET_PROD_USER, SG_PROD_USER, SG_PROD_HOST
 from config.secrets.passwords import TARGET_PROD_PWD, TARGET_TEST_PWD, SG_PROD_PWD
 from sshtunnel import SSHTunnelForwarder
 import psycopg2

 def _create_engine_psg(user, db, host, port, pwd):
 """ Returns a connection object to PostgreSQL """
 url = build_postgres_url(db, host, port, pwd, user)
 return create_engine(url, client_encoding='utf8')
 def build_postgres_url(db, host, port, pwd, user):
 url = 'postgresql://{}:{}@{}:{}/{}'.format(user, pwd, host, port, db)
 return url
 def postgres_connexion(env):
  if env == 'prod':
  return create_engine_psg_with_tunnel_ssh(TARGET_PROD_DB, 
  TARGET_PROD_USER, TARGET_PROD_PWD, SG_PROD_PWD,
                                         SG_PROD_USER, 
   SG_PROD_HOST, TARGET_PROD_HOST)
  else:
    raise ValueError("'env' parameter must be 'prod'.")

config.py

  c.NotebookApp.contents_manager_class = "jupytext.TextFileContentsManager"
  c.ContentsManager.default_jupytext_formats = "ipynb,py"

I red that i can generate the file and then edit it.

when i tried to create the jupyter_notebook_config it is always in my personal directory of marczhr

  /Users/marczhr/.jupyter/jupyter_notebook_config.py 

but i want to be done in my something that i can push on git.

Hope that im clear ^^

Thank you,

Don't run the notebook from the directory with the configuration file.

The reason is that there is an import with a config module or package in the code listed. By launching the notebook from the directory with the configuration file, it will import that Jupyter configuration file, instead of the correct package or module, with the resulting error.

Instead, run it from somewhere else, or put the configuration file elsewhere.

Or perhaps best, take the two configuration lines and add them to the end of your /Users/marczhr/.jupyter/jupyter_notebook_config.py file, then remove the 2-3 line config.py file.

In the latter case, you can now launch the notebook server from anywhere, and you don't need to specify any configuration file, since Jupyter will automatically use the generated (with added lines) one.

If you want to keep the config.py file, then launch the Jupyter notebook server from another directory, and simply specify the full path, like

jupyter --config=$HOME/SG/notebooks/config.py

All in all, this is a classic nameclash upon import, because of identically named files/directories. Always be wary of that.

(I've commented on some other potential problems in the comments: that still stands, but is irrelevant to the current problem here.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM