简体   繁体   English

python 导入路径不匹配 windows 10

[英]python importing path not matching windows 10

I im using Git bash to open jupyter lab and a notebook file.我正在使用 Git bash 打开 jupyter 实验室和笔记本文件。 I want to import a file such as test.py, with a function such as test_func(x).我想导入一个文件,如 test.py,带有一个 function,如 test_func(x)。 The test.py is in another folder then the working directory. test.py 位于另一个文件夹中,然后是工作目录。 using pwd in the notebook i get something like "C:\Users\Documents\Code_folder\".在笔记本中使用 pwd 我得到类似“C:\Users\Documents\Code_folder\”的东西。 I have added the path of the test.py using sys.path.insert(1, "C:\Users\Code\), where the test.py is located.我使用 test.py 所在的 sys.path.insert(1, "C:\Users\Code\) 添加了 test.py 的路径。

I then have no issues with importing the module, but if i add another module, test_func2(y), and i say run test.test_func2??, i cant find the function, and when running test.test_func??, i see that the output on line: File: "c:\users\code\".然后我对导入模块没有任何问题,但是如果我添加另一个模块 test_func2(y),并且我说运行 test.test_func2??,我找不到 function,并且在运行 test.test_func?? 时,我看到了output 上线:文件:“c:\users\code\”。 I belive is the lower case of the File that gets me the missing module.我相信是 File 的小写字母让我丢失了模块。

Why does this happen, and can i change it in a simple say without changing all my codes?为什么会发生这种情况,我可以在不更改所有代码的情况下简单地更改它吗?

Edit: test_func2 is another function in test.py编辑:test_func2 是 test.py 中的另一个 function

This may simply be an issue with how you're importing.这可能只是您如何导入的问题。 I'm not sure of the internal mechanics of Jupyter, but in a terminal window if you change the module it has to be reload ed (reimported.) In Python3 the reload was moved to the imp module.我不确定 Jupyter 的内部机制,但在终端 window 中,如果您更改模块,则必须reload (重新导入)。在 Python3 中,重新加载已移至imp模块。

See stackoverflow:How do I unload (reload) a module?请参阅stackoverflow:如何卸载(重新加载)模块?

For Jupyter, I assume you have the import test.py in a previous window.对于 Jupyter,我假设您在之前的 window 中有import test.py test.py。 If you add a function to a .py file, just go back to that window and rerun the import...although I'm not sure that will guarantee a reload (since just re-running the command import test.py in the terminal Python would not work.)如果您将 function 添加到.py文件中,只需将 go 回到该 window 并重新运行import test.py ...尽管我不确定在终端中重新运行命令是否会保证重新加载测试Python不起作用。)

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

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