I have a project directory structure like below
|project
|-__init__.py
|-src
|- __init.py__
|- features
|- __init.py__
|- clean_data.py
|-notebooks
|- notebook.ipynb
The main directory is called project under which I have two directories- src and notebooks.
I want to import the module clean_data.py under features directory (which is under src) in my notebook.ipynb file.
I tried this:
from ..src.features import clean_data
since all directories are serving as package with init.py file in each of them.
But it throws an error. Have spent quite a lot of effort in trying to figure this out but not sure why I am getting the error. As per this article too, I seem to be accessing the module correctly
mportError Traceback (most recent call last)
<ipython-input-23-11fd29e06b4c> in <module>()
----> 1 from ..src.features import clean_data
ImportError: attempted relative import with no known parent package
This is a part of my code, look at this:
from domain_pricing.domains import *
from domain_pricing.conversion_rate import *
I'm importing domains.py and conversion_rate.py from domain_pricing folder.
What you should do is:
from src.features import clean_data
from src.data import another_module
You do not need . or .. as Unix-Based systems pathing directories. You need to call the folder directly.
Related
So, I encountered a ModuleNotFoundError when trying to import a module in a subpackage that imports another subpackage under its directory (so it's a subsubpackage to the main directory). I have put empty __init__.py files under both the subdirectory and subsubdirectory. The code was run in Python 3.9.7.
Here's what the structure looks like:
|- main.py
|- subpackage/
|- __init__.py
|- submod.py
|- subsubpackage/
|- __init__.py
|_ subsubmod.py
The code
In main.py, I have:
from subpackage import submod
def main():
x = submod.test_func(3)
print(x)
if __name__ == 'main':
main()
and in submod.py, I want to import subsubmod.py under subsubpackage/, so I have:
from subsubpackage import subsubmod
def test_func(a):
return subsubmod.addone(a)
and finally, in subsubmod.py:
def addone(x):
return x+1
The error message:
Now if I run main.py, I got
Traceback (most recent call last):
File "/Users/anonymous/test/main.py", line 1, in
<module>
from subpackage import submod
File "/Users/anonymous/test/subpackage/submod.py",
line 1, in <module>
from subsubpackage import subsubmod
ModuleNotFoundError: No module named 'subsubpackage'
My question and confusion
I'm not sure what I have done wrong. I realized that submod.py can be run separately, so it seems that the issue occurs when the import goes down more than one subdirectory? I wonder if there's a way around this issue, or should I just use a different structure to organize my scripts.
Putting a dot before the package name worked for me.
from .subsubpackage import subsubmod
Looks like when you are in a package if you don't use relative import it will look for your packages somewhere else.
You can find more information here:
Python documents about import system
StackOverflow question about relative imports
This question keeps showing up on Stack Overflow and I can understand why. So today I took the time to create a nested project with 1 top module and 2 nested sub modules both with classes in them. And another class was also created in the top module folder. This is also considered a module by python. But it's in the same folder as the top module.
The skeleton nested module project is now live on Github at:
HdlHelpers Nested Modules Skeleton Project
I am currently developing some packages with multiple subfolders. However, when I try to import one function from a subfolder into another one, the VS code shows green but when I run it, the terminal says ModuleNotFoundError. The folder structure is shown below.
Folder
|-Subfolder_1
|-- __init__.py
|-- fitting.py
|- Subfolder_2
|-- __init__.py
|-- process.py
|- main.py
When I import subfolder_1 to main.py, it works fine, but when I try to import fitting.py to process.py, the problem occurs. I tried to mess around with the settings in vscode but nothing worked. I also tried to just put them in the same subfolder, but when I do that, the process.py functions properly but main.py throws an error for modules not found since I use process.py in the main.py.
If anyone has some idea of how to fix it, it would be greatly appreciated.
EDIT 1:
Tried the sys.path.append for importing and the same error occurred.
Traceback (most recent call last):
File "path/to/file/subfolder_2/process.py", line 6, in <module>
import fitting
ModuleNotFoundError: No module named 'fitting'
EDIT 2: When I move fitting.py into the same subfolder with process.py and import fitting, the code runs and behave properly (folder structure after the traceback message). However, when I call functions in process.py in main.py by import, this error occurred:
Traceback (most recent call last):
File "/path/to/main.py", line 4, in <module>
from subfolder_1 import process
File "/path/to/subfolder_1/process.py", line 4, in <module>
import Fitting
ModuleNotFoundError: No module named 'Fitting'
Folder
|- Subfolder_1
|-- __init__.py
|-- process.py
|-- fitting.py
|- main.py
EDIT 3: I am currently working with a workaround for this by having fitting.py and process.py in the same subfolder but not calling process.py within main.py and have another file for the functions used for main.py; however, I would like to know why is this error occurring and what can fit it since it would be great for future reference if I have to revert back to the old structure.
If you are using virtual environment then you need activate it.
for Mac OS / Linux
source <path to env>/bin/activate
for Windows
<path to env>\Scripts\activate
You can try adding a path. However, it should be noted that vscode searches for files with the workspace as the root directory.
Here I assume that you have a workspace that contains your directory structure.
You can try:
import sys
sys.path.append(".\Folder\Subfolder_1")
import fitting
Example:
Here my_python is my workspace. And it involves a project named mainfloder like your **Floder**.
import sys
sys.path.append(".\mainfloder\projectfloder\pages")
from webpages import SignUpPage
print(SignUpPage())
I can get it.
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.
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
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.
I have a Pycharm project with the following tree:
-- Sources
|--__init__.py
|--Calculators
|--__init__.py
|--Filters.py
|--Controllers
|--__init__.py
|--FiltersController.py
|--Viewers
|--__init__.py
|--DataVisualization.py
|--Models
|--__init__.py
|--Data
Where my Filters.py calls:
import Sources.Models.Data as Dt
My FilterController.py calls:
import Sources.Calculators.Filters as Fs
import Sources.Models.Data as Dt
And so on.
First, my __init__.py is actually my main. Its imports look like this:
import Calculators.Filters as Fs
import Controllers.FiltersController as Fc
import Models.Data as Dt
import Viewers.DataVisualization as Dv
I don't think that my __init__.py working as my main is correct, but it was the way I could do __Filters.py__ and __FilterController.py__ find Sources. When I run this with Pycharm it works great. However, when I run __init__.py in terminal, I got a bunch of errors like this one:
Traceback (most recent call last):
File "__init__.py", line 10, in <module>
import Calculators.Filters as Fs
File "/Users/paulaceccon/PycharmProjects/UncertaintyQuantificationOfFilters/Sources/Calculators/Filters.py", line 6, in <module>
import Sources.Models.Data as Dt
ImportError: No module named Sources.Models.Data
I am wondering how I could do this properly, in order to have the correct Python way to use packages and to be able to run it in terminal without Pycharm.
Thank you in advance.
You hava an typo. In the folders, you named it as "Model". But when importing, you named it as "Models". In addition, you need to add the directory to Python's searching paths:
package = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
sys.path.append(package)
You should add these lines in the outest init.py
I have gone through many Python relative import questions but I can't understand the issue/get it to work.
My directory structure is:
Driver.py
A/
Account.py
__init__.py
B/
Test.py
__init__.py
Driver.py
from B import Test
Account.py
class Account:
def __init__(self):
self.money = 0
Test.py
from ..A import Account
When I try to run:
python Driver.py
I get the error
Traceback (most recent call last):
from B import Test
File "B/Test.py", line 1, in <module> from ..A import Account
ValueError: Attempted relative import beyond toplevel package
This is happening because A and B are independent, unrelated, packages as far as Python is concerned.
Create a __init__.py in the same directory as Driver.py and everything should work as expected.
In my case adding __init__.py was not enough. You also have to append the path of the parent directory if you get module not found error.
root :
|
|__SiblingA:
| \__A.py
|
|__SiblingB:
| \_ __init__.py
| \__B.py
|
To import B.py from A.py, you have to do the following
import sys
# append the path of the parent directory
sys.path.append("..")
from SiblingB import B
print("B is successfully imported!")
The reason for
ValueError: Attempted relative import beyond toplevel package
is that A is the same directory level as Driver.py. Hence, ..A in from ..A import Account is beyond top-level package.
You can solve this by create a new folder named AandB together with __init__py in this new folder, and then move A and B folders into AandB folder. The directory structure is as following:
Correspondingly, the content in Driver.py should be modified as from AandB.B import Test.