繁体   English   中英

jupyter 笔记本:NameError:未定义名称“c”

[英]jupyter notebook: NameError: name 'c' is not defined

每次我尝试启动我的笔记本时,我都会收到以下错误。 让我们指定项目的新工作人员,并且在我加入团队之前创建了文件 config.py。

请问有人知道怎么解决吗?

实际完成的代码是

需求.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

错误详情

~/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

代码

导致笔记本错误的行是

  from src.util.connect_postgres import postgres_connexion

文件 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'.")

配置文件

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

我红了我可以生成文件然后编辑它。

当我尝试创建 jupyter_notebook_config 时,它总是在我的 marczhr 个人目录中

  /Users/marczhr/.jupyter/jupyter_notebook_config.py 

但我想在我可以推动 git 的事情上完成。

希望我清楚^^

谢谢,

不要从包含配置文件的目录运行笔记本。

原因是在列出的代码中有一个带有配置模块或包的导入。 通过从包含配置文件的目录启动笔记本,它将导入该 Jupyter 配置文件,而不是正确的包或模块,并导致错误。

相反,从其他地方运行它,或者将配置文件放在其他地方。

或者也许最好,将两个配置行添加到/Users/marczhr/.jupyter/jupyter_notebook_config.py文件的末尾,然后删除 2-3 行config.py文件。

在后一种情况下,您现在可以从任何地方启动笔记本服务器,并且不需要指定任何配置文件,因为 Jupyter 将自动使用生成的(添加了行)的配置文件。

如果你想保留config.py文件,那么从另一个目录启动 Jupyter notebook 服务器,只需指定完整路径,比如

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

总而言之,这是导入时的经典名称冲突,因为文件/目录具有相同的名称。 对此始终保持警惕。

(我在评论中评论了其他一些潜在问题:这仍然存在,但与此处当前的问题无关。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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