0

I am trying to make my own package so that I can use the files in a different folder. This package contains a few different modules and then the main module that imports all the others inside it. For example:

Folder

|- main.py |- other.py |- something.py |- __init__.py 

Inside the main.py I have the imports:

import other import something 

and it works just fine when running the file itself; however, I added the __init__.py file and tried to import it into a different folder. The package is recognized, but the main.py gives me the following error:

Exception has occurred: ModuleNotFoundError No module named 'univariate'

File "C:...\stats.py", line 8, in import univariate

File "F:...\testing.py", line 7, in from stats import stats

For clarification, the actual main file is called stats.py. This is my first experience trying to make a package so I might be missing something. Thank you.

1

2 Answers 2

3

You need to change your imports into relative imports

import .other import .something 

or to change it to absolute imports rooted to your project folder

import x.y.other import x.y.something 

you can read here about the imports

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the response. I did that with all the imports and now for some reason the line "from stats import stats" is giving me a syntax error
It was a syntax error, but apparently changing all the imports to stats.moduleName instead of .moduleName is fixing it. It seems most of my errors have to do with mistaking relative paths when I had to use absolute or vice-versa. I should be able to fix it now, thank you!
1

When you have a module that you're trying to import you don't need the ".py" part.

Having a folder with a init.py file (even a blank one) means that a project that contains that folder can import from it.

/myproject | - /mymodule | - |- stats.py | - |- other.py | - |- something.py | - |- __init__.py | - main.py 

then in main.py all you need to do is import mymodule or from mymodule import stats

I always hate to FTFM someone, but here's a link to how to build packages from the official documentation. But, where this really starts to shine is when you need to package your module so that someone else can run it Digital Ocean has a pretty good tutorial here.

1 Comment

thanks for the information, but my issue is not with importing the package itself, but imports inside the package. I had to convert those into relative imports, but now I get a syntax error on the line "from stats import stats" (the package is called stats and has a stats file inside).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.