Python:导入文件夹的符号链接 [英] Python: import symbolic link of a folder

查看:168
本文介绍了Python:导入文件夹的符号链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一些Python文件的文件夹A和__init __。py。

I have a folder A which contains some Python files and __init__.py.

如果我将整个文件夹A复制到其他文件夹B并在那里创建一个文件使用导入A,它可以工作。但现在我删除文件夹并移动到原始文件夹的符号链接。现在它不起作用,说没有名为foo的模块。
有谁知道如何使用符号链接进行导入?

If I copy the whole folder A into some other folder B and create there a file with "import A", it works. But now I remove the folder and move in a symbolic link to the original folder. Now it doesn't work, saying "No module named foo". Does anyone know how to use symlink for importing?

推荐答案

Python不会检查你的文件是否是符号链接或不!您的问题可能在于重命名模块或在搜索路径中没有它们!

Python doesn't check if your file is a symlink or not! Your problem lies probably in renaming the modules or not having them in your search-path!

如果ModuleA变为ModuleB并且您尝试导入ModuleA,则无法找到它,因为它不存在。

If ModuleA becomes ModuleB and you try to import ModuleA it can't find it, because it doesn't exist.

如果您将ModuleA移动到另一个目录并且生成一个带有另一个名称的符号链接(表示一个新目录),则此新目录必须是您的脚本和模块的公共父目录,或者符号链接目录必须位于搜索路径中。

If you moved ModuleA into another directory and you generate a symlink with another name, which represents a new directory, this new directory must be the common parent directory of your script and your module, or the symlink directory must be in the search path.

BTW不清楚您是指模块还是包。包含 __ init __。py 文件的目录将成为所有文件的包,其扩展名为 .py (= modules)其中。

BTW it's not clear if you mean module or package. The directory containing the __init__.py file becomes a package of all files with the extension .py (= modules) residing therein.

DIRA
  + __init__.py    <-- makes DIRA to package DIRA
  + moduleA.py     <-- module DIRA.moduleA

移动和符号链接

/otherplace/DIRA  <-+
                    |  points to DIRA
mylibraries/SYMA  --+  symbolic link

如果SYMA具有相同的名称由于DIRA和你的脚本在SYMA目录中,它应该可以正常工作。如果没有,那么你必须:

If SYMA has the same name as DIRA and your script is in the directory SYMA then it should just work fine. If not, then you have to:

import sys
sys.path.append('/path/to/your/package/root')

如果你想从你的SYMA软件包中导入一个模块,你必须:

If you want to import a module from your package SYMA you must:

import SYMA.ModuleA

简单:

import SYMA

会将包名称,而不是包中的模块导入您的命名空间!

will import the packagename, but not the modules in the package into your namespace!

这篇关于Python:导入文件夹的符号链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆