unresolved import for module imports in Visuial Studio Code - visual-studio-code

I have opened a folder in VS code and I am trying to set it up.
It's a python project and its directory structure is as:
Project
├── common_features
│ ├── ...
├── core
│ ├── features
│ └── main.py
│ └── tests
├── django project
│ ├── django_app1
│ ├── manage.py
│ ├── ...
└── tests
│ ├── ...
└── runner.py
The project runs as a django project from the django_project dir. It uses modules located in common_features and core. Core is designed such that it could also run on its own. You can also run core from runner.py
The problem is that all our module imports are not being resolved but 3rd party packages work well.
unresolved import 'core.config' Python(unresolved-import)
In PyCharm, I have marked Project, core and django_project as "sources root" and it works like a charm. Not sure how to do that in VS code.
I have tried making some changes in launch.json and settings.json but none are working. I am new to VS code so I'm unable to understand what it is that I'm doing wrong.
Thanks.

Can you try adding the following line to your settings.json file?
{
"python.autoComplete.extraPaths": ["./src"]
}
More info about this here: https://github.com/microsoft/python-language-server/blob/master/TROUBLESHOOTING.md#unresolved-import-warnings

Related

Testdriven.io: Scalable FastAPI Applications on AWS. pytest not found main module

while i try to this course https://testdriven.io/courses/scalable-fastapi-aws/
i have some problems.
when i type
talk-booking/services/talk_booking $ poetry run pytest tests/integration
error raises
=============================================== test session starts ================================================
platform darwin -- Python 3.11.1, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /Users/blarblar/Workspace/talk-booking/services/talk_booking
plugins: cov-4.0.0, anyio-3.6.2
collected 1 item / 1 error
====================================================== ERRORS ======================================================
___________________________ ERROR collecting tests/integration/test_web_app/test_main.py ___________________________
ImportError while importing test module '/Users/blarblar/Workspace/talk-booking/services/talk_booking/tests/integration/test_web_app/test_main.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/local/Cellar/python#3.11/3.11.1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py:126: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
tests/integration/test_web_app/test_main.py:4: in <module>
from web_app.main import app
E ModuleNotFoundError: No module named 'web_app'
============================================= short test summary info ==============================================
ERROR tests/integration/test_web_app/test_main.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
================================================= 1 error in 0.29s =================================================
my directory is
.
├── README.md
└── services
└── talk_booking
├── poetry.lock
├── pyproject.toml
├── tests
│ ├── __init__.py
│ └── integration
│ └── test_web_app
│ ├── __init__.py
│ └── test_main.py
└── web_app
├── __init__.py
└── main.py
i check health-check api work well. 127.0.0.1:8000
when i write test code in main.py, it works well.
if i resolve imporing issue, that will be perfect

Import File Mismatch in pytest with same test names

This is a much asked question, but none of the solutions mentioned on SO have worked so far.
The folder structure is as follows:
project/
└── tests/
├── conftest.py
├── __init__.py
└── int_tests/
└── test_device.py
└── project_core/
└── tests/
├── conftest.py
├── __init__.py
└── int_tests/
└── test_device.py
import file mismatch:
imported module 'test_device' has this __file__ attribute:
/home/.../project/project_core/tests/int_tests/test_device.py
which is not the same as the test file we want to collect:
/home/.../project/tests/int_tests/test_device.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules
Steps tried so far:
Removing pycache and pyc files.
Adding _init to each folder. (As is stated in pytest GIP)
Removing _init from each folder.
Do i need init files in each tests/subfolder?
The same error occurs with conftest.py as well. This error is not limited to vscode-pytest plugin, also occurs on the terminal.
PS : For CI purposes, the system is configured with docker & tox. Development is done in venv.

How to use my own common files in keycloak theme?

I would like to use common resources in my keycloak theme but import feature is for extand existing theme but how to use common in my theme ?
I would like to import my own common files but according to the documentation the import only allows to use the common files of another theme but how to use it without extending on another theme ?
My structure
.
├── META-INF/
│ └── keycloak-themes.json
└── theme/
└── my-custom-theme/
├── account/
│ ├── *.ftl
│ └── theme.properties
├── common/
│ └── resources/
│ └── css/
│ └── bootstrap.min.css
└── login
In my theme.properties (login)
locales=en,fr
styles=css/login.css
# which I already tried
# -> by default keycloak use common/keycloak (but how to change to my own common resources ? I tried this import=common/my-custom-theme but I have an error 500)
stylesCommon=css/bootstrap.min.css (it's bootstrap file of keycloak (3.4.1 :/)
Anyone have an idea ?
I solved it by adding the following to my theme.properties:
import=common/my-custom-theme
styles=css/bootstrap.min.css
# stylesCommon=css/bootstrap.min.css <-- not working
Tested it with Keycloak server version 15.0.2
To include in 'stylesCommon', please check if you have following in template.ftl
<#if properties.stylesCommon?has_content>
<#list properties.stylesCommon?split(' ') as style>
<link href="${url.resourcesCommonPath}/${style}" rel="stylesheet" />
</#list>
</#if>

How to access templates part of a package from a script within a package

I have trouble creating a package with setuptools. I have a repository which I'm cleaning up to make it a package. The directory structure looks something like this
my-proj
├── setup.py
├── MANIFEST.in
├── MakeFile
├── README.rst
├── setup.py
└── myproj
├── __init__.py
├── my_code.py
├── templates
│ ├── template1.yaml
│ ├── template2.yaml
Initial version of "my_code.py" had code snippet which would directly reference the files withing templates folder to do some processing. If I package this using setup tools, I provide the following information in these files:
MANIFEST.in:
include README.rst
include requirements.txt
include LICENSE.txt
recursive-include myproj/templates *
setup.py:
setup(
name='myproj',
package_dir={'testbed_init': 'testbed_init'},
package_data={'templates': ['templates/*'], 'configs': ['configs/*']},
include_package_data=True,
)
My question is as follows. In "my_Code.py" I used to reference templates directly without any problem as I would run script from the myproj folder. If I package this, how can I make sure, I include the templates as part of package and when script runs, I need to open the templates relative to where the package is installed.
Code snippet from my_code.py:
if _type == "a":
temp_file = f"templates/template1.yaml"
else:
temp_file = f"templates/template2.yaml"
build_config(deploy_esx_file, output_file, data)
Code snippet of what happens in build_config:
def build_config(template_file, output_file, inputs):
templateLoader = jinja2.FileSystemLoader(searchpath="./")
templateEnv = jinja2.Environment(loader=templateLoader)
template = templateEnv.get_template(template_file)
outputText = template.render(inputs)
with open(output_file, 'w') as h:
h.write(outputText)

Ionic2 folder structure missing app folder

I'm starting to learn Ionic2, I have create a new empty project with ionic start myproject blank --v2 and everything works correctly if I do ionic serve.
Then I read this tutorial and many others. All of them state that the ionic folder structure should have an app folder:
If I look at my project folder structure I can't see any app folder. My structure looks so:
.
├── hooks
├── plugins
├── scss
├── www
│ ├── index.html
│ ├── css
│ ├── img
│ ├── js
│ └── lib
├── bower.json
├── config.xml
├── gulpfile.js
├── ionic.project
└── package.json
Now I'd like to follow this tutorial in order to build my first application but as you can see the folder structure of that example is different from mine and as an Ionic beginner I'm a little confused.
FIY (if it matters):
I'm referring to this page for the templates.
cordova -v: 6.2.0
ionic -v: 1.7.15
It might because your Node.js needs update.
First use node --version in your terminal (if you use Mac) to check the version of your Node.js. I had the same problem with you when node version in my Mac was v5.10.1 and solved it by using brew upgrade node to upgrade my Node.js to v6.3.1.
After updating your Node.js to the latest version just use ionic start myProject --v2 to create a new project and then the new project you created should have /app inside.