简体   繁体   English

Python 3 导入函数

[英]Python 3 Importing Functions

I have imported functions before successfully, but the following has left me confused.我之前已经成功导入了函数,但是下面的让我很困惑。 Maybe, I just don't know enough about importing in python (I have tested the following principle on my own machine (Mac) and found that it works. I created fome functions with nothing but print statements and called them exactly the same way.).也许,我只是对在 python 中导入不够了解(我在自己的机器(Mac)上测试了以下原理,发现它有效。我创建了 fome 函数,只使用打印语句并以完全相同的方式调用它们。 )。

I am running a script (train.py) that import functions from a file in the /src/models/models.py.我正在运行一个脚本 (train.py),它从 /src/models/models.py 中的文件导入函数。 Using from src.models.models import * /src and train.py are in the same directory.使用from src.models.models import * /src 和 train.py 在同一目录中。

However, everytime I run python train.py I get ModuleNotFoundError: No module named 'src'.但是,每次我运行python train.py ,我都会得到 ModuleNotFoundError: No module named 'src'。

(source of the script github ) https://github.com/aildnont/covid-cxr/blob/master/src/train.py (脚本来源githubhttps://github.com/aildnont/covid-cxr/blob/master/src/train.py

Same directory hierarchy and syntax works on my own computer (again I used some simple functions with print statements).相同的目录层次结构和语法适用于我自己的计算机(我再次使用了一些带有打印语句的简单函数)。 I am running train.py on a linux remote, using Python 3 on both machines.我在 linux 遥控器上运行 train.py,在两台机器上都使用 Python 3。 Any idea?任何想法? Thanks!谢谢!

Use relative imports:使用相对导入:

from models.models import *

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM