简体   繁体   English

如何在 Jupyter 笔记本中导入 python 代码

[英]How to import python code in Jupyter notebook

I'm trying to import a python code that has usernames, passwords, and auth keys saved to a Jupyter notebook without sharing that information directly in the code.我正在尝试导入一个 python 代码,该代码将用户名、密码和身份验证密钥保存到 Jupyter 笔记本中,而不直接在代码中共享该信息。 These are just for tests but I've tried using:这些仅用于测试,但我尝试过使用:

   import sys 
   sys.path.insert(0, '/C:/user/phlf/saved_data/config.py')
   import file

I also tried我也试过

   %run "~/C:user/phlf/saved_data/Config.py"

Is there a way to import python file for Jupyter to use without posting the code directly in notebook?有没有办法导入 python 文件供 Jupyter 使用,而无需直接在笔记本中发布代码?

sys.path contains directories, not paths to file. sys.path包含目录,而不是文件的路径。

You'd probably just want to use importlib if you know which file you want to import.如果您知道要导入哪个文件,您可能只想使用importlib

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

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