英文:
Importing a module from a different path in the same project
问题
以下是翻译好的部分:
我有以下的项目结构:
-项目
-src
-package1
-script1.py
-__init__.py
-package2
-script2.py
- __init__.py
-__init__.py
现在,script1.py
包含两个函数:
def my funct1():
print("Hello")
def my funct2():
// 做些什么
现在,我想在 script2.py
中运行 funct1()
。我对这种操作比较陌生,因为我通常会把整个代码写在 main.py
中,就这样了。
我搜索了一下这个问题,然后在 script2.py
中写了以下代码:
from src.package1.script1 import *
funct1()
这给了我以下错误:
from src.package1.script1 import *
ModuleNotFoundError: No module named 'src'
请有人帮助我导入这个函数。我还有另一个问题,当我解决了导入错误后,我是直接在 script2.py
中调用 funct1()
,还是需要做一些额外的事情,比如在 script2.py
中包含一个主函数?
英文:
I have the following project structure :
-project
-src
-package1
-script1.py
-__init__.py
-package2
-script2.py
- __init__.py
-__init__.py
Now script1.py
includes two functions :
def my funct1():
print("Hello")
def my funct2():
// do something
Now I want to run funct1()
in script2.py
. I am new to something like that as I always write my whole code in main.py
and that's it.
I have searched for this problem and I wrote the following code in script2.py
:
from src.package1.script1 import *
funct1()
This gives me the following error :
from src.package1.script1 import *
ModuleNotFoundError: No module named 'src'
Somebody please help me to import the function. I have another question, when I solve the import error, do I directly call funct1()
in script2.py
or do I need to do something extra, like including a main function in script2.py
??
答案1
得分: 3
You can use relative imports like:
from ..package1.script1 import *
since src
has its own __init__.py
, it's all the same module.
This will work if calling script2
as a module, i.e.
python3 -m project.src.package2.script2
rather than
python3 project/src/package2/script2.py
In the latter case, you'll get the error ImportError: attempted relative import with no known parent package
.
If this wasn't the case, i.e. package1
and package2
are separate modules, you should just add the module path to sys.path
by including:
import os
import sys
current_file = __file__
src_directory_path = os.path.dirname(os.path.dirname(current_file))
sys.path.append(os.path.join(src_directory_path, "package1"))
in package2/__init__.py
. Then you can call things like
import package1
For more on relative imports, see the answers to https://stackoverflow.com/questions/16981921/relative-imports-in-python-3.
英文:
You can use relative imports like:
from ..package1.script1 import *
since src
has it's own __init__.py
it's all the same module.
This will work if calling script2
as a module, i.e.
python3 -m project.src.package2.script2
rather than
python3 project/src/package2/script2.py
In the later case you'll get the error ImportError: attempted relative import with no known parent package
.
If this wasn't the case, i.e. package_1
and package2
are separate modules, you should just add the module path to sys.path by including:
import os
import sys
current file = __file__
src_directory_path = os.path.dirname(os.path.dirname(current file))
sys.path.append(os.path.join(src_directory_path, "package1"))
in package2/__init__.py
. Then your call things like
import package1
For more on relative imports see the answers to https://stackoverflow.com/questions/16981921/relative-imports-in-python-3
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论