设置 python 路径程序化

Setting python path programmatic

目标:-

我想在我的项目中设置 python 路径编程。这样,所有其他目录文件都可以毫无问题地导入。

问题陈述:-

我曾经在 PYTHONPATH 下的环境变量中添加所有文件夹和子文件夹,但这有两个限制。

1、任何新创建的文件夹都需要始终添加到环境变量中。一定限制后,由于字符限制,环境变量将不再接受新的路径引用。

- 错误:这个环境变量太大了。该对话框允许 设置值最长为 2047 个字符。

2,其次,我团队中的所有团队成员都需要始终手动执行相同的 activity。

尝试过自己的解决方案:-

创建了一个示例文件夹并在主要导入文件之前添加了以下代码,并且在命令提示符下 运行 运行得非常好。

**FilePath: foo/Python/Learning&Development/Pratice/Directory1/File1.py**

class File1():

    def parent(self):
        return "I am from File1"

**FilePath: foo/Python/Learning&Development/Pratice/Directory2/File2.py**

    import sys
try:
    sys.path.index('foo/Python/Learning&Development')
    sys.path.index('foo/Python/Learning&Development/Pratice')
    sys.path.index('foo/Python/Learning&Development/Pratice/Directory1')
    sys.path.index('foo/Python/Learning&Development/Pratice/Directory2')
except ValueError:
    sys.path.append('foo/Python/Learning&Development')
    sys.path.append('foo/Python/Learning&Development/Pratice')
    sys.path.append('foo/Python/Learning&Development/Pratice/Directory1')
    sys.path.append('foo/Python/Learning&Development/Pratice/Directory2')

from Pratice.Directory1.File1 import File1 as f


class File2():

    def child(self):
        return f.parent(self)

结果:

现在我希望将 sys.path 转换为单个方法并在 运行 项目文件夹中的任何 .py 文件之前自动调用。

所以,在跟踪的基础上,我创建了'init.py'文件,添加了相同的代码片段(sys.path),在[=中注释了相同的代码68=] 和 运行 它但最终出现文件导入错误。

**FilePath: foo/Python/Learning&Development/Pratice/Directory2/__init__.py**

import sys
try:
    sys.path.index('foo/Automation/Python/Learning&Development')
    sys.path.index('foo/Automation/Python/Learning&Development/Pratice')
    sys.path.index('foo/Automation/Python/Learning&Development/Pratice/Directory1')
    sys.path.index('foo/Automation/Python/Learning&Development/Pratice/Directory2')
except ValueError:
    sys.path.append('foo/Automation/Python/Learning&Development')
    sys.path.append('foo/Automation/Python/Learning&Development/Pratice')

sys.path.append('foo/Automation/Python/Learning&Development/Pratice/Directory1') sys.path.append('foo/Automation/Python/Learning&Development/Pratice/Directory2')

**FilePath: foo/Python/Learning&Development/Pratice/Directory1/File1.py**

class File1():

    def parent(self):
        return "I am from File1"

**FilePath: foo/Python/Learning&Development/Pratice/Directory2/File2.py**

# import sys
# try:
    # sys.path.index('foo/Python/Learning&Development')
    # sys.path.index('foo/Python/Learning&Development/Pratice')
    # sys.path.index('foo/Python/Learning&Development/Pratice/Directory1')
    # sys.path.index('foo/Python/Learning&Development/Pratice/Directory2')
# except ValueError:
    # sys.path.append('foo/Python/Learning&Development')
    # sys.path.append('foo/Python/Learning&Development/Pratice')
    # sys.path.append('foo/Python/Learning&Development/Pratice/Directory1')
    # sys.path.append('foo/Python/Learning&Development/Pratice/Directory2')

from Pratice.Directory1.File1 import File1 as f


class File2():

    def child(self):
        return f.parent(self)

有人可以帮我解决这个问题吗?我希望在 运行 在我的项目文件夹中的任何 .py 文件之前执行相同的代码。这样就不会出现导入错误。

预期:Python 路径应默认自动设置,并且在 运行 项目中的任何 python 文件时不应出现导入错误。

解决方案是设置默认 python 路径,运行 每个 python 文件。添加新文件或文件夹时,您不需要将文件添加到环境变量。 只需简单地在我的系统中配置环境变量 e-g windows 我添加 C:\Anaconda\python.exe 你只需将路径添加到你的 python.exe 文件所在的环境变量。

您需要在每个目录中创建一个文件init.py。 (2 个前导下划线和 2 个尾随下划线。) 您只需要将顶级目录包含在 sys.path 中,就可以使用从子目录中导入内容 topdir.subdir

我通过在环境变量中添加最顶层的文件夹并将其设置为 pycharm 中的源根目录来解决这个问题。另外,在各自的文件夹结构中从源根目录导入文件。