如何从一个文件夹导入另一个文件夹中的另一个Python文件

huangapple go评论91阅读模式
英文:

How do I import another python file from folder to another folder

问题

I have here a python folder:
Inside is another 2 folders namely: session_9 and session_10
Here's what it looks like:
python

  • session_9
    1. main.py
  • session_10
    1. file1.py

我这里有一个Python文件夹:
里面有另外两个文件夹,分别是:session_9 和 session_10
看起来是这样的:
python

  • session_9
    1. main.py
  • session_10
    1. file1.py

I just want to import file1.py to my session_9 folder so that I can use it in my main.py file.
THANK YOU IN ADVANCE..

我只想将file1.py导入到我的session_9文件夹中,以便我可以在main.py文件中使用它。
提前谢谢..

I'd try:

  • from session_10.file1.py import sampfunc as sp # sampfunc is a samplefunction inside file1.py
  • import sys
    sys.path.insert('D:\pythonfiles\python_session\python_session\session_10

我会尝试:

  • from session_10.file1.py import sampfunc as sp # sampfunc是file1.py中的一个示例函数
  • import sys
    sys.path.insert('D:\pythonfiles\python_session\python_session\session_10
英文:

I have here a python folder:
Inside is another 2 folders namely: session_9 and session_10
Here's what it looks like :
python

  • session_9
    1. main.py
  • session_10
    1. file1.py

I just want to import file1.py to my session_9 folder so that I can use it in my main.py file.
THANK YOU IN ADVANCE..

I'd try :

  • from session_10.file1.py import sampfunc as sp # sampfunc is a samplefunction inside file1.py
  • import sys
    sys.path.insert('D:\pythonfiles\python_session\python_session\session_10

答案1

得分: 1

Python会将你正在运行的主脚本的父文件夹添加到PYTHONPATH/sys.path中,因此默认情况下只有在session_9目录下声明的模块或包才能被发现。

首先,通过创建一个名为__init__.py的空文件来声明session_10是一个包。

然后,更新PYTHONPATH/sys.path以指示包的位置。

你可以在main.py脚本的前两行中使用sys.path来完成这个操作。

import sys 
sys.path.insert(0, 'D:\pythonfiles\python_session\python_session')

然后,你就可以使用以下语句导入模块:

from session_10.file1 import sampfunc
英文:

Python adds the parent folder of the main script that you are running to the PYTHONPATH / sys.path, so only the modules or packages declared under the session_9 directory will be discoverable by default.

First, declare that session_10 is a package by creating an empty file named __init__.py

Then update the PYTHONPATH / sys.path to indicate where the package is located.

You can do this in the first two lines of the main.py script with sys.path.

import sys 
sys.path.insert(0, 'D:\pythonfiles\python_session\python_session')

Then you should be able to import the module with the following statement:

from session_10.file1 import sampfunc

答案2

得分: 0

你可以在Python中这样做:

import sys
sys.path.append(0, 'pythonfiles\python_session\python_session\session_10')
from file1 import #你在file1.py中拥有的任何函数
英文:

You can do something like this in python:

import sys
sys.path.append(0, 'pythonfiles\python_session\python_session\session_10')
from file1 import #whatever function you have in file1.py

huangapple
  • 本文由 发表于 2023年7月10日 13:51:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76650950.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定