附加Pythonpath,从windows上的不同目录导入模块
我一直在努力学习Python,我被困在示例48中。在示例47中,我必须创建如下所示的目录:附加Pythonpath,从windows上的不同目录导入模块,python,windows,import,python-3.4,pythonpath,Python,Windows,Import,Python 3.4,Pythonpath,我一直在努力学习Python,我被困在示例48中。在示例47中,我必须创建如下所示的目录: skeleton |--ex47 |--module.py |--__init__.py |--tests |--ex47_tests.py |--__init__.py 从现在起,我必须将ex47/module.py导入tests/ex47_tests.py。我收到“没有名为ex47的模块”错误。此问题的解决方案是通过在module.py中添加两行代码,将ex47目录的路径添加
skeleton
|--ex47
|--module.py
|--__init__.py
|--tests
|--ex47_tests.py
|--__init__.py
从现在起,我必须将ex47/module.py导入tests/ex47_tests.py。我收到“没有名为ex47的模块”错误。此问题的解决方案是通过在module.py中添加两行代码,将ex47目录的路径添加到站点包中:
import sys
sys.path.append('./ex47')
这很有效。我可以将module.py导入ex47_tests.py,并且可以将其导入计算机上的任何位置
在移动到示例48之后,我创建了完全相同的目录和文件,我将路径添加到ex48/中,并且我一直收到“没有名为48的模块”
。我在互联网上搜索了不同的解决方案,但没有一个有效。将\uuuu init\uuuuu.py
添加到骨架中没有帮助
这个问题是超级基本的问题,但是它并没有介绍给新的python程序员。
顺便说一句,我想要一个解决方案,它可以在任何使用我的代码的计算机上运行
这些问题在Linux中会发生吗?您需要看到的是从哪里调用python程序。 我有以下文件
C:\Users\kumarvivek\Desktop>tree /f skeleton
Folder PATH listing for volume ????
Volume serial number is 6AE1-4919
C:\USERS\KUMARVIVEK\DESKTOP\SKELETON
│ __init__.py
│
├───ex47
│ mod.py
│ mod.pyc
│ __init__.py
│ __init__.pyc
│
└───tests
ex47_tests.py
__init__.py
C:\Users\kumarvivek\Desktop>
包括以下内容:
C:\Users\kumarvivek\Desktop>type skeleton\ex47\mod.py
import os
x = "C:\\Users\\kumarvivek\\Desktop\\skeleton\\ex47\\module.py"
directoryPath= os.path.dirname(x)
fileName = os.path.basename(x)
print "\nFilePath: %s\nDirectoryPath: %s\nFileName: %s\n" %(x, directo
ryPath, fileName)
C:\Users\kumarvivek\Desktop>
及
您所需要看到的就是从何处调用python程序。 我有以下文件
C:\Users\kumarvivek\Desktop>tree /f skeleton
Folder PATH listing for volume ????
Volume serial number is 6AE1-4919
C:\USERS\KUMARVIVEK\DESKTOP\SKELETON
│ __init__.py
│
├───ex47
│ mod.py
│ mod.pyc
│ __init__.py
│ __init__.pyc
│
└───tests
ex47_tests.py
__init__.py
C:\Users\kumarvivek\Desktop>
包括以下内容:
C:\Users\kumarvivek\Desktop>type skeleton\ex47\mod.py
import os
x = "C:\\Users\\kumarvivek\\Desktop\\skeleton\\ex47\\module.py"
directoryPath= os.path.dirname(x)
fileName = os.path.basename(x)
print "\nFilePath: %s\nDirectoryPath: %s\nFileName: %s\n" %(x, directo
ryPath, fileName)
C:\Users\kumarvivek\Desktop>
及