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

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

`pandas.read_csv` with `IPython` path problem

问题

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

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

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

# root @ 49a10d67a5d5 in /code on git:main x [15:12:53] 
$ tree
.
|-- errata
|   |-- test.ipynb
|   `-- top_funds.py
|-- skew_all.csv
`-- test.ipynb

1 directory, 4 files

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

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

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

英文:

Here is my ipython_config.py

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

Here is my file structure

# root @ 49a10d67a5d5 in /code on git:main x [15:12:53] 
$ tree
.
|-- errata
|   |-- test.ipynb
|   `-- top_funds.py
|-- skew_all.csv
`-- test.ipynb

1 directory, 4 files

Here is my ~/.profile grep

# root @ 49a10d67a5d5 in /code on git:main x [16:00:59] 
$ cat ~/.profile| grep PY
export PYTHONPATH=$PYTHONPATH:/code
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.
# root @ 49a10d67a5d5 in /code on git:main x [14:42:01] 
$ ipython
Python 3.8.10 (default, Mar 13 2023, 10:26:41) 
Type 'copyright', 'credits' or 'license' for more information
IPython 8.12.2 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import sys

In [2]: sys.path
Out[2]: 
['/usr/local/bin',
 '/code',
 '/usr/lib/python38.zip',
 '/usr/lib/python3.8',
 '/usr/lib/python3.8/lib-dynload',
 '',
 '/usr/local/lib/python3.8/dist-packages',
 '/usr/lib/python3/dist-packages',
 '/code']

In [3]: pd.read_csv('skew_all.csv')
Out[3]: 
           date  skew_510300
0    2019-12-23    -0.097785
1    2019-12-24    -0.091429
2    2019-12-25    -0.111135
3    2019-12-26    -0.104044
4    2019-12-27    -0.151558
..          ...          ...
808  2023-04-26     0.035485
809  2023-04-27     0.008492
810  2023-04-28     0.024342
811  2023-05-04     0.049912
812  2023-05-05     0.030516

[813 rows x 2 columns]

In [4]: from errata.top_funds import *

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.
# root @ 49a10d67a5d5 in /code/errata on git:main x [14:46:20] 
$ ipython
Python 3.8.10 (default, Mar 13 2023, 10:26:41) 
Type 'copyright', 'credits' or 'license' for more information
IPython 8.12.2 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import sys

In [2]: sys.path
Out[2]: 
['/usr/local/bin',
 '/code/errata',
 '/code',
 '/usr/lib/python38.zip',
 '/usr/lib/python3.8',
 '/usr/lib/python3.8/lib-dynload',
 '',
 '/usr/local/lib/python3.8/dist-packages',
 '/usr/lib/python3/dist-packages',
 '/code']

In [3]: pd.read_csv('skew_all.csv')
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
Cell In[3], line 1
----> 1 pd.read_csv('skew_all.csv')
many tracebacks
FileNotFoundError: [Errno 2] No such file or directory: 'skew_all.csv'

In [4]: from errata.top_funds import *

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:

确定