admin管理员组文章数量:1389903
I have a directory named models that contains python files. I created a .py
file named test and tried to import a function from my module named program. Here is the code in the test.py:
from program import say_hello
print(say_hello())
When I run the test.py file, it tells me ModuleNotFoundError: No module named program
My program directory has a __init__.py
file in it.
How can I help python recognize my local python module ‘program’ as a module? I am using the online version of replit as an IDE.
I need python to look in the root directory of LyMedia for my local python module ‘program’, so I can import functions from it.
It only works when I put the models directory in the root of LyMedia and use this code:
import sys
import os
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../')))
from program import say_hello
print(say_hello())
I have a directory named models that contains python files. I created a .py
file named test and tried to import a function from my module named program. Here is the code in the test.py:
from program import say_hello
print(say_hello())
When I run the test.py file, it tells me ModuleNotFoundError: No module named program
My program directory has a __init__.py
file in it.
How can I help python recognize my local python module ‘program’ as a module? I am using the online version of replit as an IDE.
I need python to look in the root directory of LyMedia for my local python module ‘program’, so I can import functions from it.
It only works when I put the models directory in the root of LyMedia and use this code:
import sys
import os
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../')))
from program import say_hello
print(say_hello())
Share
Improve this question
asked Mar 14 at 13:28
Cameron HorneCameron Horne
93 bronze badges
4
|
1 Answer
Reset to default 0You can add a setup.py file outside of the models folder:
from pathlib import Path
from setuptools import setup, find_packages
setup(
name="models",
version="1.0.0",
package_dir={"": "models"},
packages=find_packages(where="models"),
)
Then you can run (from the directory in which the setup.py file and the models directory are):
pip install -e .
And finally in your files in models you can import using the following (start with .. if in a subfolder in models):
from .<filename> import <func>
from .<sub_folder>.<filename> import <class>
Or from outside of the models folder:
from models.<filename> import <func>
For details see: setuptools (example)
本文标签: replitPython is not recognizing my module as a moduleStack Overflow
版权声明:本文标题:replit - Python is not recognizing my module as a module - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744654048a2617856.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
sys.path
in your program. Ensure that the proper directory is on the search path when you first runpython
. – chepner Commented Mar 14 at 13:50test.py
andprogram.py
should be in the same directory. – chepner Commented Mar 14 at 13:53