VS code not recognise a python module - python

I am testing VS code and I like it very much. But I have an issue with a Python module.
The module is xspec (https://heasarc.gsfc.nasa.gov/docs/xanadu/xspec/python/html/index.html). The issue is that VS code does not recognize the module, underlining in red all the functions.
This is probably because xspec needs to be initialized before using it. To inizialize it, an entire software has to be initialized (https://heasarc.gsfc.nasa.gov/docs/software/heasoft/). Basically, every time I need the xspec module I have to inizialized the heasoft sofwtware before running python, otherwise the module is not recognized.
There is a way to solve it? Or there is a way to add exceptions to the VS code highlining errors?

Related

How to test whether the dependencies installed?

I've been learning PyTorch for deep learning recently.
Using anaconda I found some problems when I ran the program.
For example, I encountered the following import error
"no module named kiwisolver"
when my program imported matplotlib. It is fixed, but such error is very frustrating. The program runs for a long time.
Is there any way to check whether all the required dependencies are installed?
Depending on how your program is structured...
Many Python programs use the if __name__ == "__main__": idiom so that they don't immediately execute code. This lets you import the code without it immediately running.
For example, if you have my_py_torch.py, then if you run python to launch the Python interpreter in interactive mode, you can import your code:
import my_py_torch
Importing your code will process any imports, execute any top-level code, and define any functions and classes, but, as long as you use the if __name__ == "__main__": idiom, it won't actually run the (long-running) code. That's typically enough to let you know if you have major issues like syntax errors, bad imports, or missing dependencies.
Code can still circumvent this: you may have functions or methods that only import modules locally (when they're actually run), or code may wrap imports in try / except blocks to handle missing dependencies then later throw an error if the dependency is used. So it's not foolproof, but it can be a useful test.

Jython fails to load Scikit Module [duplicate]

I want to call python from the java code and pass the parameters to function in python. I have encapsulated a function in python. But the thing is I have also imported third party library sklearn in the python program. When I call a test program and pass the value to python, I am able to run the code. But when I tried using third party library such as sklearn and import in the python code, I got following error.
ImportError: No module named sklearn
I initialized object for the class PythonInterpreter and then using the following command, I included the path for the sklearn
this.interpreter.exec("import sys");
this.interpreter.exec("sys.path.append(\"/usr/local/lib/python2.7/dist-packages/sklearn\")");
Can anyone tell how to fix this problem? How to include third party library in the jython code and run the program?
As sklearn depends on native CPython extensions, it currently won't work with Jython. However, keep an eye on JyNI (www.jyni.org), which will vastly improve this issue, but is still in an early state. Until JyNI is sufficiently mature, you can use one of the following projects:
JEP (https://github.com/mrj0/jep) or
JPY (https://github.com/bcdev/jpy).
They work by embedding the CPython interpreter and don't integrate with Jython. Still, if you depend on things like scikit or numpy, these are currently the only workable approaches to use your Python-code from Java.

Detect all missing imports in PyCharm/Python

Is there a simple way, preferably in PyCharm (2017.1) but via command-line python (3.5) if necessary, to detect all code places where a statement is referring to an unresolved reference, e.g. because an import statement is missing?
I am new to Python/PyCharm. More generally, any syntax errors or anything in a similar vein would be a bonus. All I am looking for is the kind of errors I would get if I were "compiling" and "linking" in another language.
I have looked at Can PyCharm list all of Python errors in a project? and PyCharm's "Inspect Code". It is way more complex than I had in mind (and takes ages to run). I see that Python Rope: How to Find all missing imports and errors in all sub modules refactoring recommends pylint, but I wasn't looking for lint-like. I just want darn-obvious errors!
I am tasked with porting a fair-size (32K lines) application, which (apparently) runs under Windows, to Linux. The first thing I want to do is get rid of some of the imports all over the place. If my application executes a line which then has an unresolved reference I get a runtime error, but I want to pick them all up at edit-time. And there will be paths of code which are Windows-only, but I still want to know of any errors like this.
To answer my own question:
From Can PyCharm list all of Python errors in a project?, you can indeed use Code|Inspect Code to get all these errors/warnings in PyCharm as a list where you can click to get to the code. It does take a long time, but at least it's built into PyCharm, and the errors reported correspond to what you see in the editor window.
Pycharm will list all errors and 'warnings' for each source file at the right-hand side of the editor window.
They are represented as short lines or small blocks, depending on the size of the error or 'warning'. Errors are shown in red.
You click on them to take you to the place of the problem in the source.
Warnings are mostly Python style-guide violations (PEP 8).
Another option is to use pylint. This is lint for Python. This will detect missing imports.
You can integrate it into PyCharm by following the instructions in https://stackoverflow.com/a/46409649/4459346

Python spyder debug freezes with circular importing

I have a problem with the debugger when some modules in my code call each other.
Practical example:
A file dog.py contains the following code:
import cat
print("Dog")
The file cat.py is the following:
import dog
print("Cat")
When I run dog.py (or cat.py) I don't have any problem and the program runs smoothly.
However, when I try to debug it, the whole spyder freezes and I have to kill the program.
Do you know how can I fix this? I would like to use this circular importing, as the modules use functions that are in the other modules.
Thank you!
When I run dog.py (or cat.py) I don't have any problem and the program runs smoothly.
AFAICT that's mostly because a script is imported under the special name ("__main__"), while a module is imported under it's own name (here "dog" or "cat"). NB : the only difference between a script and a module is actually loaded - passed an argument to the python runtime (python dog.py) or imported from a script or any module with an import statement.
(Actually circular imports issues are a bit more complicated than what I describe above, but I'll leave this to someone more knowledgeable.)
To make a long story short: except for this particular use case (which is actually more of a side effect), Python does not support circular imports. If you have functions (classes, whatever) shared by other scripts or modules, put these functions in a different module. Or if you find out that two modules really depends on each other, you may just want to regroup them into a single module (or regroup the parts that depend on each other in a same module and everything else in one or more other modules).
Also: unless it's a trivial one-shot util or something that only depends on the stdlib, your script's content is often better reduced to a main function parsing command-line arguments / reading config files / whatever, importing the required modules and starting the effective process.

What happens when a Python program exits and a library is loaded using ctypes?

I have tried to find some information on this, but nothing so far. The scenario is a Python program that uses a C based library loaded with ctypes and then encounters an unhandled exception. Will Python unlioad the library? Or will it matter?
The main reason why this is an issue is that the library in question is a device driver, and I want to avoid leaving the hardware in the wrong state should something go wrong.

Categories