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 Avoid modifying sys.path in your program. Ensure that the proper directory is on the search path when you first run python. – chepner Commented Mar 14 at 13:50
  • For your original code to work, test.py and program.py should be in the same directory. – chepner Commented Mar 14 at 13:53
  • 1 How are you running your test file exactly? – deceze Commented Mar 14 at 14:02
  • 1 Before importing your module, list the files in the current directory. – kaksi Commented Mar 14 at 14:08
Add a comment  | 

1 Answer 1

Reset to default 0

You 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