I have these eggs:
~/test/lib/
├── a-1.0-py2.7.egg
│ ├── a
│ │ ├── __init__.py
│ │ └── __init__.pyc
│ └── EGG-INFO
│ └── ...
├── a.b-1.0-py2.7.egg
│ ├── a
│ │ └── b
│ │ ├── __init__.py
│ │ └── __init__.pyc
│ └── EGG-INFO
│ └── ...
├── easy-install.pth
├── site.py
└── site.pyc
a/__init__.py is:
print "a"
a/b/__init__.py is:
print "a.b"
So, "a.b" is a "plugin" for "a". I would install it separately (as most others).
But in that configuration my idea doesn't work:
>>> import a
a
>>> import a.b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named b
>>>
How it must be?
check your generated egg-file, if there is a module a.b.
If not, try do use find_packages to register your modules.
from setuptools import setup, find_packages
setup(
name='pypack',
version='0.1',
packages=find_packages(),
...
├── a.b-1.0-py2.7.egg
│ ├── a
│ ├── __init__.py
│ └── __init__.pyc
│ │ └── b
│ │ ├── __init__.py
│ │ └── __init__.pyc
│ └── EGG-INFO
│ └── ...
each folder should have an
__init__.py
Related
This is my project structure:
.
├── connectapp
│ ├── __init__.py
│ ├── __pycache__
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── djangoproject
│ ├── __init__.py
│ ├── __pycache__
│ ├── asgi.py
│ ├── djangoproject.sqlite3
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── djangoproject.sqlite3
├── hackerapp
│ ├── Controllers
│ ├── Models
│ ├── Serializers
│ ├── __init__.py
│ ├── __pycache__
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ └── tests.py
├── manage.py
└── requirments.txt
When I do below being in top level folder:
python3 manage.py migrate hackerapp
I am getting this:
djangoproject/djangoproject/urls.py", line 19, in <module>
from ..hackerapp.Controllers import PersonViewSet, DepartmentViewSet
ImportError: attempted relative import beyond top-level package
To me looks import should work but it's not, can someone tell me why?
I guess (?) migrate hackerapp are cli params to the manage.py script. The error means that the top level folder is not a package. To make it into a package add an (empty) __init__.py file then from that folder run:
python -m manage migrate hackerapp # note no .py
I am trying to importing
fethcer . py file from
src/fetcher/entrypoints/fethcer.py
to tests/steps/step_impl.py file
how can i import that?
...src.fetcher.entrypoints.fetcher import *
but it's giving me error
from ...src.fetcher.entrypoints.fetcher import *
ImportError: attempted relative import with no known parent package
then what is the way?
dependency graph is -
.
├── fetcher.db
├── README.MD
├── src
│ └── fetcher
│ ├── entrypoints
│ │ ├── fetcher.py
│ │ ├── __init__.py
│ │ └── __pycache__
│ │
│ │
│ ├── __init__.py
│ └── __pycache__
│
└── tests
├── acceptance
│ └── fetch_relevant_instrument_list.feature
├── environment.py
└── steps
└── steps_impl.py
This should work:
steps_impl will have following line:
import sys
sys.path.insert(1, r'path/to/the/application/')
from src.fetcher.fetcher import func1
I tried to recreate your folder structure:
D:.
├───src
│ ├───fetcher
│ │ └───__pycache__
│ │ └───__init__.py
│ │ └───fetcher.py
│ ├───__pycache__
│ └───__init__.py
└───tests
├───acceptance
└───steps
└───steps_impl.py
I refered this Question.
ImportError while loading conftest '/home/rohit/flask/src/tests/conftest.py'.
conftest.py:6: in <module>
from mycode import create_app
E ModuleNotFoundError: No module named 'mycode'
when running pytest form the src folder above mycode folder, it gives ModuleNotFoundError
The directory structure is as follow
├── mycode
│ ├── auth
│ ├── auth.py
│ ├── db_insert.py
│ ├── db.py
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── auth.cpython-37.pyc
│ │ ├── db.cpython-37.pyc
│ │ ├── db_insert.cpython-37.pyc
│ │ └── __init__.cpython-37.pyc
│ ├── README.md
│ ├── schema.sql
│ └── templates
│ ├── auth
│ │ ├── login.html
│ │ └── register.html
│ └── base.html
└── tests
├── conftest.py
├── data.sql
├── __pycache__
│ └── conftest.cpython-37-pytest-5.4.1.pyc
└── test_factory.py
Your __init__.py file needs to go in the folder named mycode. If the above is already true then try the following.
Rename __init__.py to mycode.py
You can see here for more solutions that may solve your problem.
I have a tree that looks like this:
└──env
│
└──Project
│
├── DirA
│ ├── A_MAIN
│ ├── __init__.py
│ ├── FILES
│ └── __init__.py
│ └── fileA1
│ └── fileA2
│
├── FoldersB
│ └── DirB
│ ├── A_MAIN
│ ├── __init__.py
│ ├── FILES
│ └── __init__.py
│ └── fileA1
│ └── fileA2
├── Tests
│ └── test.py
│ └── __init__.py
│
├── __init__.py
In (both) fileA1 I have something similar to:
from A_MAIN.FILES.fileA2 import <CLASS>
For whatever reason however, the top fileA1 is importing the implementation from FoldersB/../../../fileA2, instead of the fileA2 in its same directory.
It may be important to note that all the folder names, class names, etc are the same. My hunch is something is going wrong in the sys.path, but I'm having trouble debugging it.
sys.path:
[
'/Users/Shared/FolderX/FolderY/Project/Tests',
'/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/lib/python36.zip',
'/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/lib/python3.6',
'/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload',
'/Users/Shared/FolderX/FolderY/env/lib/python3.6/site-packages',
'/Users/Shared/FolderX/FolderY/Project/FoldersB/DirB',
'/Users/Shared/FolderX/FolderY',
'/Users/Shared/FolderX/FolderY/Project/DirA/A_MAIN/irrelevant_package'
]
From inside DirA.../fileA1:
import A_MAIN.FILES.fileA2 as testImportPath
print(os.path.abspath(__file__))
# Results in:
# /Users/Shared/FolderX/FolderY/Project/DirA/A_MAIN/FILES/fileA1
print(os.path.abspath(testImportPath.__file__))
# Results in:
# /Users/Shared/FolderX/FolderY/Project/FoldersB/DirB/A_MAIN/FILES/fileA2
I want to be able to compare the implementations of CLASS in both fileA1's (who contain fileA2's CLASS as member variables) from the Project directory, which is why I'm running into this strange environment.
The current working directory is /Tests/test.py.
EDIT: Updated tree to be more correct and included sys paths and os path information
My project layout is as follows:
├ ...
├── pve
│ ├── blahblah
│ │ ├── TestDefinition.py
│ │ ├── TestDefinition.pyc
│ │ ├── __init__.py
│ │ └── __init__.pyc
│ └── pve.py
├── src
│ └── definitions
│ └── THISFILE.yml
└── ...
I need to be able to fetch files (THISFILE.yml for example) from src/definitions by the pve/blahblah/TestDefinition.py class.
How do I properly access the project root? Once I have that, I can access the .yml files relative.
TIA.
I like to create some sort of configuration file in the project root that has an aboslute path to the root. I do this only because the frameworks i usually use (django, scrapy) have some sort of convention like this
├ ...
├── pve
│ ├── blahblah
│ │ ├── TestDefinition.py
│ │ ├── TestDefinition.pyc
│ │ ├── __init__.py
│ │ └── __init__.pyc
│ └── pve.py
├── src
│ └── definitions
│ └── THISFILE.yml
└── settings.py
# settings.py
import os
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
DEFINITIONS_ROOT = os.path.join(PROJECT_ROOT, 'src', 'definitions')
from myproject import settings
settings.DEFINITIONS_ROOT