`pandas.read_csv` 与 `IPython` 路径问题

huangapple go评论86阅读模式
英文:

`pandas.read_csv` with `IPython` path problem

问题

以下是您的 ipython_config.py 文件的翻译部分:

  1. $ cat ~/.ipython/profile_default/ipython_config.py
  2. c = get_config()
  3. c.InteractiveShellApp.exec_lines = [
  4. 'import sys',
  5. 'sys.path.append("/code")',
  6. 'import pandas as pd',
  7. ]

以下是您的文件结构的翻译部分:

  1. # root @ 49a10d67a5d5 in /code on git:main x [15:12:53]
  2. $ tree
  3. .
  4. |-- errata
  5. | |-- test.ipynb
  6. | `-- top_funds.py
  7. |-- skew_all.csv
  8. `-- test.ipynb
  9. 1 directory, 4 files

以下是您的 ~/.profile 中的 grep 结果的翻译部分:

  1. # root @ 49a10d67a5d5 in /code on git:main x [16:00:59]
  2. $ cat ~/.profile| grep PY
  3. export PYTHONPATH=$PYTHONPATH:/code
  4. export JUPYTER_PATH=$JUPYTER_PATH:/code

希望这些翻译对您有帮助。如果您需要更多翻译或有其他问题,请随时提问。

英文:

Here is my ipython_config.py

  1. $ cat ~/.ipython/profile_default/ipython_config.py
  2. c = get_config()
  3. c.InteractiveShellApp.exec_lines = [
  4. 'import sys',
  5. 'sys.path.append("/code")',
  6. 'import pandas as pd',
  7. ]

Here is my file structure

  1. # root @ 49a10d67a5d5 in /code on git:main x [15:12:53]
  2. $ tree
  3. .
  4. |-- errata
  5. | |-- test.ipynb
  6. | `-- top_funds.py
  7. |-- skew_all.csv
  8. `-- test.ipynb
  9. 1 directory, 4 files

Here is my ~/.profile grep

  1. # root @ 49a10d67a5d5 in /code on git:main x [16:00:59]
  2. $ cat ~/.profile| grep PY
  3. export PYTHONPATH=$PYTHONPATH:/code
  4. export JUPYTER_PATH=$JUPYTER_PATH:/code

I want to read file /code/skew_all.csv and import modules in /code. Things are strange.

  1. If I use ipython in the terminal with working directory /code, I can easily read it and import modules in /code.
  1. # root @ 49a10d67a5d5 in /code on git:main x [14:42:01]
  2. $ ipython
  3. Python 3.8.10 (default, Mar 13 2023, 10:26:41)
  4. Type 'copyright', 'credits' or 'license' for more information
  5. IPython 8.12.2 -- An enhanced Interactive Python. Type '?' for help.
  6. In [1]: import sys
  7. In [2]: sys.path
  8. Out[2]:
  9. ['/usr/local/bin',
  10. '/code',
  11. '/usr/lib/python38.zip',
  12. '/usr/lib/python3.8',
  13. '/usr/lib/python3.8/lib-dynload',
  14. '',
  15. '/usr/local/lib/python3.8/dist-packages',
  16. '/usr/lib/python3/dist-packages',
  17. '/code']
  18. In [3]: pd.read_csv('skew_all.csv')
  19. Out[3]:
  20. date skew_510300
  21. 0 2019-12-23 -0.097785
  22. 1 2019-12-24 -0.091429
  23. 2 2019-12-25 -0.111135
  24. 3 2019-12-26 -0.104044
  25. 4 2019-12-27 -0.151558
  26. .. ... ...
  27. 808 2023-04-26 0.035485
  28. 809 2023-04-27 0.008492
  29. 810 2023-04-28 0.024342
  30. 811 2023-05-04 0.049912
  31. 812 2023-05-05 0.030516
  32. [813 rows x 2 columns]
  33. In [4]: from errata.top_funds import *
  34. In [5]:
  1. If I use ipython in the terminal with working directory /code/errata, I can NOT read it BUT can import modules in /code.
  1. # root @ 49a10d67a5d5 in /code/errata on git:main x [14:46:20]
  2. $ ipython
  3. Python 3.8.10 (default, Mar 13 2023, 10:26:41)
  4. Type 'copyright', 'credits' or 'license' for more information
  5. IPython 8.12.2 -- An enhanced Interactive Python. Type '?' for help.
  6. In [1]: import sys
  7. In [2]: sys.path
  8. Out[2]:
  9. ['/usr/local/bin',
  10. '/code/errata',
  11. '/code',
  12. '/usr/lib/python38.zip',
  13. '/usr/lib/python3.8',
  14. '/usr/lib/python3.8/lib-dynload',
  15. '',
  16. '/usr/local/lib/python3.8/dist-packages',
  17. '/usr/lib/python3/dist-packages',
  18. '/code']
  19. In [3]: pd.read_csv('skew_all.csv')
  20. ---------------------------------------------------------------------------
  21. FileNotFoundError Traceback (most recent call last)
  22. Cell In[3], line 1
  23. ----> 1 pd.read_csv('skew_all.csv')
  24. many tracebacks
  25. FileNotFoundError: [Errno 2] No such file or directory: 'skew_all.csv'
  26. In [4]: from errata.top_funds import *
  27. In [5]:
  1. If I use Jupyter notebook in VS Code with working directory /code, I can NOT read it too, BUT can import modules in /code.
    This is strange and unusual. But can be solved by setting Notebook File Root to ${workspaceFolder} thanks to @MingJie-MSFT

This file is /code/test.ipynb and screenshot was took with working directory of /code.

`pandas.read_csv` 与 `IPython` 路径问题

  1. If I use Jupyter notebook in VS Code with working directory /code/errata, I can NOT read it too, BUT can import modules in /code.

And this can NOT be solved by setting Notebook File Root to ${workspaceFolder}, with working dir of /code/errata, but can be solved with working dir of /code.

This file is /code/errata/test.ipynb and screenshot was took with working directory of /code.

`pandas.read_csv` 与 `IPython` 路径问题

So the basic conclusion is path is working because of the successful module import, but read_csv not.

I guess there is something unclear with pandas.read_csv path handling. And probably some unknown difference between Jupyter notebook in VS Code and terminal ipython

My goal is to run

pd.read_csv('skew_all.csv')

and

from errata.top_funds import *

smoothly without writing any addition codes. The solution I've taken is the ipython_config.py but it has the above strange things.

答案1

得分: 1

你可以参考这个答案

打开你的设置,搜索Notebook File Root

然后将其更改为${workspaceFolder},并重新加载VSCode。

`pandas.read_csv` 与 `IPython` 路径问题

看起来在更新后,它会根据文件所在的目录作为根目录运行。

英文:

You can refer to this answer.

Open your settings and search for Notebook File Root

Then change it to ${workspaceFolder} and reload VSCode.

`pandas.read_csv` 与 `IPython` 路径问题

It seems that after updating, it runs based on the directory where the file is located as the root directory.

huangapple
  • 本文由 发表于 2023年5月17日 14:57:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269305.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定