使用不同的计算机时,无需更改路径即可从位于不同目录中的文件导入函数
Importing functions from files located in a different directory when working with different computers without the need of changing the path
我开发了一个小程序file_1.py,现在正在开发它的测试file_2.py。它们在不同的文件夹中。为了进行测试,我想将函数从 file_1 导入到 file_2。要从位于不同文件夹中的文件导入函数,我见过这种方法:
# file_2.py
import sys
sys.path.append('/.../application/app/folder')
from file_1 import *
application
├── app
│ └── folder
│ └── file_1.py
└── app2
└── some_folder
└── file_2.py
该程序将供不同计算机上的客户端使用,我想编写 file_2.py 的代码,以便他们只需编写 python file_2.py
即可 运行 进行测试]
我怎样才能做到这一点而不需要在每台不同的计算机上修改路径?所有机器都是Mac或Linux.
更新:
这是我正在使用的真实代码的副本:
from VCF_matcher.app.run import *
NAME_FILE_1 = "./test_sample.vcf"
# FIRST TEST
def test_load_sample():
data = load_sample (NAME_FILE_1)
assert len(data) == 10425
您可以在 file2.py
:
from app.folder.file_1 import *
我开发了一个小程序file_1.py,现在正在开发它的测试file_2.py。它们在不同的文件夹中。为了进行测试,我想将函数从 file_1 导入到 file_2。要从位于不同文件夹中的文件导入函数,我见过这种方法:
# file_2.py
import sys
sys.path.append('/.../application/app/folder')
from file_1 import *
application
├── app
│ └── folder
│ └── file_1.py
└── app2
└── some_folder
└── file_2.py
该程序将供不同计算机上的客户端使用,我想编写 file_2.py 的代码,以便他们只需编写 python file_2.py
即可 运行 进行测试]
我怎样才能做到这一点而不需要在每台不同的计算机上修改路径?所有机器都是Mac或Linux.
更新:
这是我正在使用的真实代码的副本:
from VCF_matcher.app.run import *
NAME_FILE_1 = "./test_sample.vcf"
# FIRST TEST
def test_load_sample():
data = load_sample (NAME_FILE_1)
assert len(data) == 10425
您可以在 file2.py
:
from app.folder.file_1 import *