更改用户名后 Astropy 无法下载文件

更改用户名后 Astropy 无法下载文件

我按照这个帖子更改了我的用户名:我如何更改我的用户名?

这是几周前的事了,我刚刚遇到了第一个问题,Astropy(适用于 Python 2.7)在下载文件时遇到问题,因为它在错误的缓存文件夹中查找。错误消息:IOError:[Errno 2] 没有这样的文件或目录:u'/home/jill/.astropy/cache/download/py2/bf6211066f3d9a9b058d46183baba6ab'。(我的旧用户名是 jill,我可以看到这个文件存在于我的新主文件夹中)。

逐行调试后,似乎错误来自“Shelve”模块。也就是说,/home/jill 是 URL 中第一次引入的地方。

我尝试重新安装 python(sudo apt-get install --reinstall python2.7)并重新安装 astropy(pip2 uninstall astropy、pip2 install astropy)。

*Pycharm 上的完整错误消息:

File "/home/jpsotka/Dropbox/Colibri/Simulation/corrections.py", line 74, in get_zenith
    elginfield_altaz = ecliptic(lat=elat*u.degree, lon=elon*u.degree).transform_to(AltAz(obstime=time, location=elginfield))

 File "/home/jpsotka/.local/lib/python2.7/site-packages/astropy/coordinates/baseframe.py", line 934, in transform_to
    return trans(self, new_frame)

File "/home/jpsotka/.local/lib/python2.7/site-packages/astropy/coordinates/transformations.py", line 1314, in __call__
    curr_coord = t(curr_coord, curr_toframe)

 File "/home/jpsotka/.local/lib/python2.7/site-packages/astropy/coordinates/transformations.py", line 914, in __call__
    return supcall(fromcoord, toframe)

 File "/home/jpsotka/.local/lib/python2.7/site-packages/astropy/coordinates/builtin_frames/cirs_observed_transforms.py", line 53, in cirs_to_altaz
    xp, yp = get_polar_motion(obstime)

 File "/home/jpsotka/.local/lib/python2.7/site-packages/astropy/coordinates/builtin_frames/utils.py", line 43, in get_polar_motion
    xp, yp, status = iers.IERS_Auto.open().pm_xy(time, return_status=True)

 File "/home/jpsotka/.local/lib/python2.7/site-packages/astropy/utils/iers/iers.py", line 600, in open
    cls.iers_table = cls.read(file=filename)

 File "/home/jpsotka/.local/lib/python2.7/site-packages/astropy/utils/iers/iers.py", line 456, in read
    iers_a = Table.read(file, format='cds', readme=readme)

  File "/home/jpsotka/.local/lib/python2.7/site-packages/astropy/table/table.py", line 2521, in read
    out = io_registry.read(cls, *args, **kwargs)

 File "/home/jpsotka/.local/lib/python2.7/site-packages/astropy/io/registry.py", line 531, in read
    data = reader(*args, **kwargs)

 File "/home/jpsotka/.local/lib/python2.7/site-packages/astropy/io/ascii/connect.py", line 39, in io_read
    return read(filename, format=format, **kwargs)

File "/home/jpsotka/.local/lib/python2.7/site-packages/astropy/io/ascii/ui.py", line 353, in read
    dat = reader.read(table)

File "/home/jpsotka/.local/lib/python2.7/site-packages/astropy/io/ascii/cds.py", line 322, in read
    return super(Cds, self).read(table)

File "/home/jpsotka/.local/lib/python2.7/site-packages/astropy/io/ascii/core.py", line 1159, in read
    self.lines = self.inputter.get_lines(table)

File "/home/jpsotka/.local/lib/python2.7/site-packages/astropy/io/ascii/core.py", line 296, in get_lines
    encoding=self.encoding) as fileobj:

File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
    return self.gen.next()

  File "/home/jpsotka/.local/lib/python2.7/site-packages/astropy/utils/data.py", line 206, in get_readable_fileobj
    fileobj = open(name_or_obj, 'rb')

IOError: [Errno 2] No such file or directory: u'/home/jill/.astropy/cache/download/py2/bf6211066f3d9a9b058d46183baba6ab'

Process finished with exit code 1

答案1

如果你正在使用 astropy 1.0.10 或更高版本,这应该可以毫无问题地清除缓存:

#! /usr/bin/env python
from astropy.utils import data
data.clear_download_cache()

文档链接:

astropy.utils.data.clear_download_cache(hashorurl=None)

[来源]

通过删除本地文件来清除数据文件缓存。

参数: 哈希尔网址字符串或者没有任何

如果为 None,则清除整个缓存。否则,指定应删除的缓存文件的哈希值,或应从缓存中删除的 URL(如果存在)。

https://docs.astropy.org/en/stable/api/astropy.utils.data.clear_download_cache.html

相关错误和拉取请求的链接:

https://github.com/astropy/astropy/issues/4427

https://github.com/astropy/astropy/pull/4810

答案2

我不知道有什么方法可以完全避免这种情况,即让 Python 跳过不存在的缓存。但是,有一种简单的方法可以让您的程序重新运行,那就是让文件夹中出现缓存(或任何资源)。您可以通过创建指向(真实)的/home/jill符号链接来执行此操作:/home/jill/home/jpsotka

sudo ln -s /home/jpsotka /home/jill

就这样。当然,如果jill系统上有另一个用户,则目录/home/jill已经存在,因此无法创建链接。

我认为过一段时间后缓存中的文件会被完全替换,然后您就可以删除该链接了:

sudo rm /home/jill 

请注意,这确实不是 删除链接指向的文件夹,仅删除链接本身。

相关内容