I am converting my python project into packages, with sub-packages, with modules. The tree structure is as shown below (sorry for the long tree). When I run run.py, it imports MyEnvironment.environment which in turn has statements like from station import Station. It is at this stage that I get an error saying ModuleNotFoundError: No module named 'station'. I am not able to figure why this is happening. Note that it work when I do sys.append('.\Environment'), but it should work without it since I already added __init__.py. Could someone help here?
C:.
├───.vscode
│ settings.json
│
└───DRLOD-Package
│ run.py
│ __init__.py
│
├───MyEnvironment
│ │ agv.py
│ │ environment.py
│ │ job.py
│ │ job_queue.py
│ │ parent_area.py
│ │ path_finding.py
│ │ pdnode.py
│ │ policy.py
│ │ request.py
│ │ station.py
│ │ stats.py
│ │ test.py
│ │ __init__.py
```
In environment.py use from .station import Station
Related
I am struggling with the implementation of unittest for subdirectories. I have the following project
project
│ README.md
│ __init__.py
│
└───common
│ __init__.py
│ └───my_func
│ │ __init__.py
│ │ func1.py
│ │ func2.py
│
└───scripts
│ __init__.py
│ └───folder_1
│ │ __init__.py
│ │ code1.py
│ │ code2.py
│
│ └───folder_2
│ │ __init__.py
│ │ code3.py
│ │ code4.py
│ │
│ └───tests
│ │ └───test1
│ │ │ __init__.py
│ │ │ test_code3.py
│ │ │ test_code4.py
│ │ │
│ │ └───test2
│ │ │ __init__.py
│ │ │ test_code3.py
│ │ │ test_code4.py
I set the working directory to be ./project. In my code1.py file I import common.my_func.func1 and it runs normally.
Now, I am trying to implement some unittest functions in ./project/scripts/tests. To do so, I am using the coverage package and running the command: coverage run --source=scripts -m unittest discover scripts/tests. However, when doing so, I get the following error:
ModeluNotFoundError: No module named common.my_func
Weirdly speaking, the scripts works perfectly when I try to run it for one test folder only, and not the whole folder coverage run --source=scripts -m unittest discover scripts/tests/test1.
I tried multiple combinations of removing the source, getting more specific with the folder and on. Have any of you faced similar problems with python 3.8?
Thank you very much in advance,
Common question, but I have not found a solution reading >10 question threads.
I have the following directory:
ParentFolder
├───notebooks
│ │ .gitkeep
│ │ 1raw_eda.ipynb
│ │ 2raw_feature_engineering.ipynb
│ │ 3advanced_eda.ipynb
│ │ 4advanced_feature_engineering.ipynb
│ │ 5modelling.ipynb
│ │ __init__.py
|
├───src
│ │ __init__.py
│ ├───features
│ │ .gitkeep
│ │ build_features.py
│ │ __init__.py
|
|__init__.py
I am in 1raw_eda.ipynb, my os.getcwd() is notebooks. If i now try import ParentFolder.src.features.build_features, I am getting a No module named 'ParentFolder' error and I don't know why as I have __init__.py everywhere.
Additional Information: ParentFolder is capitalized like this and I just changed its name from Parent-Folder to ParentFolder, in case this is relevant. I have no info whether the import worked before as I didn't try it before (actually I renamed it to allow for the import).
Thanks already!
I am trying to generate Sphinx documentation for my Python application. Originally I had a complex structure as follows...
venv
docs
├───source
├───├───_static
├───├───_templates
├───├───conf.py
├───├───index.rst
├───├───modules.rst
├───├───...
├───build
├───make.bat
├───Makefile
├───MyCode
├───├───Utilities
│ │ └───class1.py
├───├───Configurations
│ │ ├───Archive
│ │ ├───API1_Configurations
│ │ │ ├───Config1.ini
│ │ ├───API2_Configurations
│ │ │ ├───Config2.ini
│ │ ├───API3_Configurations
│ │ │ ├───Config3.ini
│ │ ├───API4_Configurations
│ │ │ ├───Config4.ini
├───├───APIs
│ │ ├───API1
│ │ │ ├───Class1.py
│ │ │ ├───Class2.py
│ │ ├───API2
│ │ │ ├───Class1.py
│ │ │ ├───Class2.py
│ │ │ ├───Supporting
│ │ │ │ └───Class1.py
│ │ ├───API3
│ │ │ ├───Support
│ │ │ │ ├───SupportPackage1
│ │ │ │ ├───Support Package2
│ │ │ │ │ └───Class1.py
│ │ │ │ └───__pycache__
│ │ │ └───Class1.py
In this case, my source code exists in ./docs/MyCode.
I am using...
Python 3.8
Sphinx 4.2 (although I've tried with many versions)
NumPy docstrings
I have...
Added the following extensions
sphinx.ext.autodoc
sphinx.ext.apidoc
sphinx.ext.napoleon
Pointed conf.py to my code using both a relative path and absolute path (relative path being ../MyCode).
For some reason, the closest I can get to actually populating the HTML pages with my documentation is simply having the classes in the index toctree. They link out to blank html pages without my Python docstrings.
Does anyone have any idea why it won't grab my docstrings??
Ok... feeling pretty dumb about this, but it was just cause of the warnings.
Sphinx requires that the package is a fully installable package in the sense that when it is placed in the sphinx package, it should be able to compile. I had some absolute references in my import statements which caused my program to fail. As such, the actual docstrings in my program could not be pulled while the names of the classes still showed in my sphinx html.
Basically make sure your code compiles when trying to throw it in Sphinx.
I got the error:
File ".\main.py", line 3, in <module>
from app.core import config
ModuleNotFoundError: No module named 'app'
in the line 3 of main.py:
from app.core import config
I have tried the left empty the file app/init.py and with the next:
from .core import config
My structure folder is:
backend
│
└───app
│ .env
│ main.py
│ __init__.py
│
├───api
│ __init__.py
│
├───core
│ config.py
│ __init__.py
│
├───db
│ base.py
│ base_class.py
│ __init__.py
│
├───db_models
│ association_tables.py
│ team.py
│ user.py
│ __init__.py
│
└───schema
│ mutation.py
│ query.py
│ __init__.py
│
├───team
│ mutations.py
│ queries.py
│ types.py
│ __init__.py
│
└───user
mutations.py
queries.py
types.py
__init__.py
I'd due to problem with absolute/relative reference. I use PyCharm and I don't have alarm about the reachable or not of the modules.
thanks for advance
I rebuilt your structure and this should do the trick:
from core import config
I am working on a project which has a structure similar to
project
│ README.md
│
└───package1
│ │ __init__.py
│ │ moduleA.py
│ │ └───classX
│ │ └───classY
│ │ moduleB.py
│
└───package2
│ │ __init__.py
│ │ moduleC.py
│ │ moduleD.py
│
└───package3
│ │ __init__.py
│ │ moduleE.py
│ │ moduleF.py
│ │
│ └───subpackage31
│ │ __init__.py
│ │ moduleG.py
│ │ moduleH.py
Inside moduleG, I am trying to import moduleC, as well as classes X and Y defined inside moduleA. I tried,
from ...package2 import moduleC
from ...package1.moduleA import classX
I get the error,
Traceback (most recent call last):
File "/project/package3/subpackage31/moduleG.py", line 17, in <module>
from ...package2 import moduleC
SystemError: Parent module '' not loaded, cannot perform relative import
Any help would be appreciated.
Thanks in advance.
This is a very old question, but Google brought me here and I thought I would share a possible solution. You could do the following:
import sys
sys.path.append('../')
from package2 import moduleC
from package1.moduleA import classX