Strange Python package import behavior in Visual Studio - python

I encountered a very strange behavior of Python project in VS 2017.
Suppose, I have a "library" Python project and also a unit testing project.
When I add the following lines
import unittest
import Metric.metric as metr // module from the first project
into file in the second project it works fine (I even can press F12 on 'unitests' and go to __init_ file of unittest framework, or on 'metr' and see contents of metric.py file) but only until VS reload.
After reloading the second line no longer works!!! The first line may work but just for a while. After some time (and may be another VS reload) it also breaks down - neither F12 nor unit test discovery not working
Moreover, after I type 'import' and press Ctrl+Space, a 'unittest' hint string also no longer available in IntelliSence popup!
What may be the reason of such a strange problem?
I also tried to reproduce it on another toy example with no success - everything works as expected! So you may also be not able to reproduce the problem. The only hope is for someone's intuition.
My settings (all up-to-date):
Visual Studio 2017 15.5.2
Environment - Python 3.6 (64 bit) (global default)

Comments in python are with # instead of //

Related

Issue with new isort extension installed as from VS-Code Update October 2022 (version 1.73)

I'm using VS-Code version 1.73.1, with MS Python extension v2022.18.2, on Windows 10 Pro, Build 10.0.19045. After installing the October 2022 update of VS Code, when writing Python code I noticed nagging error diagnostics being issued by the isort extension about the import order of modules. Previously, I had never encountered such diagnostics.
I traced this behaviour back to the VS Code release notes for the Update October 2022. These announce the migration of VS Code to a new stand-alone isort extension, instead of the isort support built into the Python extension, by automatically installing it alongside the Python extension. When opening a file in which the imports do not follow isort standards, the extension is intended to issue an error diagnostic and display a Code Action to fix the import order.
Whilst the extension seems to work as intended, I found the issues described below:
1. Even after having executed the Code Action to fix the import order, a 'light-bulb' with the same error diagnostic and Code Action again pops up on moving the cursor to a new line of code.
2. The error diagnostic and Code Action 'light-bulb' are also displayed when moving the cursor to any new line of code, even when all lines of code in the file have been commented out; that is, effectively, there are no longer any import statements in the code, and therefore also nothing to be sorted.
I'd appreciate comments on whether this is a recognised issue in VS Code, and if so, whether any workarounds are available. It defeats the purpose of having an 'error lightbulb' pop up on every line of code, just to find a code action recommending to fix the import order, even when this requires no fixing.
I have opened this question on this forum as recommended on the GitHub 'Contributing to VS Code' page.
Upgrade the isort extension version to latest(v2022.8.0).

Allure ModuleNotFoundError but only on certain functions of the package

I am starting to work with Allure to make reports for my test automation team. My goal is to attach screenshots but I run into an error. The lines of code that prompt this error are:
import allure #This line runs fine
from allure.constants import AttachmentType #This one throws the error
Which throws: ModuleNotFoundError: No module named 'allure.constants'; 'allure' is not a package
I am using allure throughout different parts of the code without running into errors, for example I use the decorator:
#allure.step("Description of a step")
So the module is being correctly loaded. Upon further inspection on the internet I found links stating that I should uninstall previous packages such as pytest-allure-adaptor as seen in this other SO question. I did so but the error remains unchanged.
In order to give more context, I am using the following softaware:
IDE: Visual Studio Code
OS: Windows 7
Python version: 3.6
allure-pytest: 2.8.12
allure-python-commons: 2.8.12
It seems that in the version of allure that I was using allure.constants does not exist.
I solved my problem my changing that line to:
from allure import attachment_type
And then, to take the screenshot and adding it to the report:
allure.attach('screenshot', driver.get_screenshot_as_png(), type=attachment_type.PNG)
I hope this helps someone else in the future.

Visual Studio Code adding lines between code in Python terminal causing syntax errors

I'm running Python code in VS Code (1.28.2, with the Python, and Python Extension Pack extensions) and am using the 'Run Selection/Line in Python Terminal' (Shift+Enter) functionality to just run selected code in the Python terminal.
This has always worked well, but today I'm getting a new line added between each line of code in the terminal, i.e. if I ran:
import heapq
import pickle
the output in the terminal would be:
>>>import heapq
>>>
>>>import pickle
At first, this just seems like an annoyance, but any for loops or functions now come out with an indentation error...so essentially I can't successfully run any code.
I've tried re-installing VS Code as well as installing an older version of VS Code but all give the same problem.
It's so odd because it was working fine and then all of a sudden it changed. The only thing I can think of that has possibly changed is I installed the JSON Tools extension, but I don't believe this would change anything within Python (and I've tried uninstalling this, and not loading it again when reinstalling VS Code from scratch)
Any help would be greatly appreciated!
It's a bug that will be fixed in the 2018.9.1 release of the Python extension.

Python module created with Boost.Python won't be imported

I have a big C++ module with Python 3 bindings using Boost.Python, that I compile to a .so file using CMake on macOS.
When I try to import it in the REPL, everything seems to work fine:
>>>import myModule
>>>
However, as soon as I run the import statement, the famous rocket icon of Python shows up in the Dock and stays there jumping for some minutes and stops after. Obviously then, I cannot access any of the functions defined in my module, so the import looks fine but doesn't actually do anything.
I tried looking in the Console and saw that whenever I import myModule, I get two launchservicesd[83]: SecTaskLoadEntitlements failed error=22.
It brought me to this and that related questions but I can't find what the exact problem is.
The C++ module is huge so I just can't look at the code and find the problem, thus I'm asking for any hints about at least how to debug that problem.
I can suggest the following steps:
Try to import that module though local python session. So, run interactive python interpreter, and 'import myModule'.
If bad, try to check:
are python version, with which myMoudle was linked with, is similiar to used interpreter
check that build architectires are the same
check that you can load even simple boost.python example module
If ok, check that you have correctly set up module search path in your python code.

Pycharm code completion works, but inserts unresolved references

Using PyCharm 3.0.1 Community the code completion works fine, but when I e.g. compelete to a method to one of the parents classes, the inserted method name then gets marked as an unresolved reference. What am I missing?
Details:
I am new to PyCharm, and test it on a (unified installed) Plone 4.3.2. The interpreter from the Installation is selected automatically, and I add the zinstance/bin/zopepy script for buildout.
I then open Products.CMFPlone-4.3.2-py2.7.egg/Products/CMFPlone/SkinsTool.py and then type
x = SkinsTool
at the bottom of the file. The code completion offers me manage_changeProperties (and shows PropertyManager). I select this, so that the line now says
x = SkinsTool.manage_changeProperties
The manage_changeProperties part is marked though. Hovering over the marked 'manage_changeProperties' now shows
Unresolved attribute reference 'manage_changeProperties' for class 'SkinsTool'
Is this a bug or am I doing something wrong (e.g. with importing the project)?
Edit: Following vape's advice I also tried on an instance. Same problem:
Auto-complete works fine:
But then the reference is unresolved:
This test was also done with 3.0.1 Professional.
Edit 2: The strucuture is the one of a standard plone installation: structure.txt. I have opened the topmost plone.4.3.2 folder.
Cheers,
Joerg
This is likely because there was an error when pycharm generated the skeletons for the plone library. Unless this is a bug, the only way to solve your problem is to delete the .idea folder inside your project. After doing so, reopen the project from the command line using charm <your project dir>. In case you have not made a command line tool for pycharm yet, this is how you do it:
Ok, the bug is fixed:
https://youtrack.jetbrains.com/issue/PY-11401
The next release will hopefully contain the fix, until then I compiled my own version.
Two things to note:
Link (or copy) the folder 'help' from the official community release into the intellij-community/python directory.
comile it from that python directory using 'ant -Didea.build.number=140.1332' (I think the actual number doesn't matter)
The result is in intellij-community/out/pycharmCE/artifacts

Categories