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
Related
I have a Python package in my Git repository called lib/requestAPI that contains a __pycache__ directory and three modules:
├── lib
│ ├── __init__.py
│ ├── requestAPI
│ │ ├── __init__.py
│ │ ├── __pycache__
│ │ ├── checkwx.py
│ │ ├── github.py
│ │ └── weatherflow.py
I have added __pycache__/ to my .gitignore file to ignore the contents of the __pycache__ directory.
I recently renamed the Python package to lib/request_api and the names of the three modules. I pushed these changes to the remote server and then pulled the changes to a second machine. On this second machine, however, the lib/requestAPI folder remains as it contains the local __pycache__ directory
├── lib
│ ├── __init__.py
│ ├── requestAPI
│ │ └── __pycache__
│ ├── request_api
│ │ ├── __init__.py
│ │ ├── __pycache__
│ │ ├── checkwx_api.py
│ │ ├── github_api.py
│ │ └── weatherflow_api.py
Is there a Git command I can use to remove this extra __pycache__ directory associated with the old package directory? I know I can use git clean -d -X to remove all ignored files and directories, but this also removes ignored configuration files which I don't want. Removing this folder manually is also not an option as the repository is distributed to many people
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.
I am currently writing a library in Python. I am trying to use a class that I have defined in a module but I am unable to use it in my main.py. Inside Selector and SeasonSelector I have 2 classes defined with the same name of the file. I get the following error:
No name 'Selector' in module 'Selectors'
main.py:
import pyf1
testSeasonSelector = pyf1.Selectors.SeasonSelector()
testSelector.loadData()
pyf1/__init__.py:
from Selectors.Selector import Selector
from Selectors.SeasonSelector import SeasonSelector
Directory
├── main.py
└── pyf1
├── Selector.pyc
├── Selectors
│ ├── SeasonSelector.py
│ ├── SeasonSelector.pyc
│ ├── Selector.py
│ ├── Selector.pyc
│ └── __init__.pyc
├── __init__.py
├── __init__.pyc
├── __pycache__
│ └── __init__.cpython-38.pyc
└── data
Your directory should look like this
├── main.py
└── pyf1
├── selector.pyc
├── selectors
| ├── __init__.py
│ ├── season_selector.py
│ ├── season_Selector.pyc
│ ├── selector.py
│ ├── selector.pyc
│ └── __init__.pyc
├── __init__.py
├── __init__.pyc
├── __pycache__
│ └── __init__.cpython-38.pyc
└── data
Your directory is not a certified package without init.py as such you cannot make import statements. I also took the liberty of correcting how you named your modules to fit good python naming convention\practices. With this you should be able to import from your built package without issues
I managed to find a solution.
The issue was I wasn't using the correct syntax. Once I added the missing __init__() files I used the import statement Import .parent_file from ParentClass Which then allowed me to pass functionality up the chain how I wanted.
Inside the Selectors directory I could then use from .selector import __Selector to use in SeasonSelector
Furthermore - I hadn't included the PyF1 in my sys.path, therefore python wasn't able to scan the directories. This included with the syntax above allowed me to do what I wanted.
I have the following directory structure in my Python3 project:
├── README.md
├── requirements.txt
├── schemas
│ ├── collector.sql
│ └── controller.sql
├── src
│ ├── controller.db
│ ├── controller.py
│ ├── measurement_agent.py
├── tests
│ ├── regression.py
│ ├── test-invalid-mac.py
│ ├── test-invalid-url.py
│ ├── test-register-ma-json.py
│ ├── test-register-ma-return-code.py
│ ├── test-send-capabilities-return-code.py
│ ├── test-valid-mac.py
│ └── test-valid-url.py
└── todo
In my tests folder I have some regression tests which are ran to check the consistency of the code from src/measurement_agent.py. The problem now is that I do not want to add to my path manually the measurement_agent.py to make an import from it. I would want to know if there is any trick how to tell Python to look in my root for the import I am trying to use.
Currently I am doing:
import os.path
ma = os.path.abspath('..') + '/src'
sys.path.append(ma)
from measurement_agent import check_hardware_address
and would want to have something just like
from measurement_agent import check_hardware_address
without using any os.path tricks.
Any suggestions?
Relative imports
Make sure there is an __init__.py in all folders including the top-most (the parent)
Use a relative import, like this:
from ..src import measurement_agent
Now to run your code, cd up to the parent of your parent directory and then
python -m parent.test.regression
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