Git remove ignored __pycache__ directory after Python package rename - python

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

Related

Dynamically create and run tests in Github Actions

I have a git project with directory structure similar to this:
.
├── .github/
│ └── workflows/
│ └── test.yml
└── my_packages/
├── package_1/
│ ├── tests/
│ │ └── test_package.py
│ ├── package_logic.py
│ ├── configurations.yml
│ └── requirements.py
├── package_2/
│ ├── tests/
│ │ └── test_package.py
│ ├── package_logic.py
│ ├── configurations.yml
│ └── requirements.py
├── ...
└── package_n/
├── tests/
│ └── test_package.py
├── package_logic.py
├── configurations.yml
└── requirements.py
There are n packages in this repository and every package is independent one from another with its own requirements.py, logic, and tests.
There is also test.yml file, which is currently not implemented, that needs to create a testing environment for every package separately and run its test (pytest). Another requirement is that every package defines its python version in the configurations.yml file in which the module test should run. And finally, the test should run only for modules which code was changed.
How can this be implemented in Github Actions? And is it possible to run every module in a separate job (or just in parallel)?

How to resolve this issue? cannot just understand , I tried to import it by install but still not working

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.

Python unable to import nested module

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.

Python3 wrong import path

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

Python - Loading files relative from project root

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

Categories

Resources