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
Related
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'm trying to replace some values from specific fields from one YAML file to another.
I have 2 different folders structures.
├── helm -> root folder
├── accounting-pl.yaml
├── Chart.yaml
├── charts
├── templates
│ ├── NOTES.txt
│ ├── _helpers.tpl
│ ├── deployment.yaml
│ ├── ingress.yaml
│ ├── service.yaml
│ └── tests
│ └── test-connection.yaml
└── values.yaml
└── unplse
├── Chart.yaml
├── charts
├── templates
│ ├── NOTES.txt
│ ├── _helpers.tpl
│ ├── deployment.yaml
│ ├── ingress.yaml
│ ├── service.yaml
│ └── tests
│ └── test-connection.yaml
└── values.yaml
....... and so son
and
├── yaml -> root folder
├── accounting-pl.yaml
├── bi-client-svc.yaml
├── bi-cltscreen.yaml
├── bi-cnsm-lispl.yaml
├── bi-cnsm-unpl.yaml
├── bi-cons-unpl.yaml
├── bi-consumers.yaml
├── bi-data-svc.yaml
├── bi-datareten.yaml
├── clapserver.yaml
├── client.yaml
├── cn-p24.yaml
├── cn-payu.yaml
├── consent.yaml
├── debt-coll-pl.yaml
├── document.yaml
├── ds-falcon-bare.yaml
├── ds-slackme.yaml
├── ds-ss-m0563c0.yaml
├── ds-ss-m18d452.yaml
├── ds-ss-m1d681c.yaml
├── ds-ss-m30b4b3.yaml
├── ds-ss-m459e80.yaml
├── ds-ss-m48a063.yaml
├── ds-ss-m50eacc.yaml
├── ds-ss-m57043d.yaml
├── ds-ss-m5fb04b.yaml
├── ds-ss-m69bc21.yaml
├── ds-ss-m6edd4b.yaml
├── ds-ss-m706b21.yaml
├── ds-ss-m7fb269.yaml
├── ds-ss-m802936.yaml
├── ds-ss-m8aa1c3.yaml
inside the values.yaml file there is the field replicaCount: 1
inside of each of this YAML files like ds-ss-m7fb269.yaml (inside the yaml root folder) there is the field desiredCount: x
How can I get the value of this field desiredCount and paste in replicaCount? E.g. replicaCount: 44
I tried many things but failed in all of them.
This is what I tried so far.
def get_replicaCount():
for root, dir, files in os.walk('.', topdown=False):
for name in files:
if name == "values.yaml":
with open(os.path.join(root, name), 'rt') as helm:
try:
lyamls = yaml.safe_load(helm)
print(lyamls['replicaCount'])
except Exception as err:
print(err)
it prints all the replicaCount values
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
...and so on
def get_desiredCount():
for i in os.listdir(path):
with open('yaml/' + i, 'r') as f:
loaded = yaml.safe_load(f)
print(loaded['service']['desiredCount'])
get all the desiredCount values and print them.
4
3
9
12
and so son...
How could I replace these values?
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
I´m trying to deploy my django 1.6.4 project on pythonAnywhere using python 2.7
I already configured a virtual enviroment and the wsgi file according to the guidelines on the website. But I´m getting a 404 when I check the site. The error lol tells me this:
ImportError: Could not import settings 'tango_with_django_project.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named tango_with_django_project.settings
Here´s my wsgi:
# +++++++++++ DJANGO +++++++++++
# TURN ON THE VIRTUAL ENVIRONMENT FOR YOUR APPLICATION
activate_this = '/home/pjestrada/.virtualenvs/rango/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
# To use your own django app use code like this:
import os
import sys
#
## assuming your django settings file is at '/home/pjestrada/mysite/settings.py'
path = '/home/pjestrada/rango/tango_with_django_project'
if path not in sys.path:
sys.path.append(path)
os.chdir(path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'tango_with_django_project.settings'
#
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
My tree:
├── Dropbox
│ ├── README.txt
│ └── __init__.py
├── README.txt
└── rango
├── LICENSE
├── README.md
└── tango_with_django_project
├── manage.py
├── media
│ ├── profile_images
│ │ └── 10411981_634016890008979_1609187547738555774_n.jpg
│ └── rango2.jpg
├── populate_rango.py
├── rango
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── admin.py
│ ├── admin.pyc
│ ├── bing_search.py
│ ├── bing_search.pyc
│ ├── forms.py
│ ├── forms.pyc
│ ├── models.py
│ ├── models.pyc
│ ├── tests.py
│ ├── urls.py
│ ├── urls.pyc
│ ├── views.py
│ └── views.pyc
├── static
│ ├── about.jpg
│ ├── css
│ │ ├── bootstrap-fluid-adj.css
│ │ ├── bootstrap-responsive.css
│ │ ├── bootstrap-responsive.min.css
│ │ ├── bootstrap.css
│ │ └── bootstrap.min.css
│ ├── img
│ │ ├── glyphicons-halflings-white.png
│ │ └── glyphicons-halflings.png
│ ├── js
│ │ ├── bootstrap.js
│ │ ├── bootstrap.min.js
│ │ ├── jquery-2.1.1.js
│ │ └── rango-ajax.js
│ └── rango.jpg
├── tango_with_django_project
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── settings.py
│ ├── settings.pyc
│ ├── urls.py
│ ├── urls.pyc
│ └── wsgi.py
└── templates
└── rango
├── about.html
├── add_category.html
├── add_page.html
├── base.html
├── category.html
├── category_list.html
├── index.html
├── login.html
├── page_list.html
├── profile.html
├── register.html
├── restricted.html
└── search.html
Try changing value of path.
path = '/home/pjestrada/rango'
That path is your project directory. It worked for me.
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