Matlab System Function and Terminal Runs differently - python

I have a python code that I want to run in Matlab. It has an `import NumPy statement in it. The code runs without a problem in the terminal. But when I use Matlab system function It gives me the import error below.
Import error: No module named numpy
I'm using Python 3.9 on Matlab 2020b
My question is, why does the system function gives a different result than the Terminal itself?
I've tried to add the Python folder to MATLABPATH. But nothing changed.
Any suggestions?
Thanks in advance.

Related

Trying to run python code on jenkins in ubuntu

all.
I recently started working with Jenkins, in an attempt to replace cronjob with Jenkins pipeline. I have really a bit knowledge of programming jargon. I learned what I learned from questions on stackoverflow. So, if you guys need any more info, I would really appreciate if you use plain English.
So, I installed the lastest version of Jenkins and suggested plugins plus all the plugins that I could find useful to python running.
Afterwards, I searched stackoverflow and other websites to make this work, but all I could do was
#!/usr/bin/env python
from __future__ import print_function
print('Hello World')
And it succeeded.
Currently, Jenkins is running on Ubuntu 16.04, and I am using anaconda3's python (~/anaconda3/bin/python).
When I tried to run a bit more complicated python code (by that I mean import pandas), it gives me import error.
What I have tried so far is
execute python script build: import pandas - import error
execute shell build: import pandas (import pandas added to the code that worked above)
python builder build: import pandas - invalid interpreter error
pipeline job: sh python /path_to_python_file/*.py - import error
All gave errors. Since 'hello world' works, I believe that using anaconda3's python is not an issue. Also, it imported print_function just fine, so I want to know what I should do from here. Change workspace setting? workdirectory setting? code changes?
Thanks.
Since 'hello world' works, I believe that using anaconda3's python is not an issue.
Your assumption is wrong.
There are multiple ways of solving the issue but they all come down to using the correct python interpreter with installed pandas. Usually in ubuntu you'll have at least two interpreters. One for python 2 and one for python 3 and you'll use them in shell by calling either python pth/to/myScript.py or python3 pth/to/myScript.py. python and python3 are in this case just a sort of labels which point to the correct executables, using environmental variable PATH.
By installing anaconda3 you are adding one more interpreter with pandas and plenty of other preinstalled packages. If you want to use it, you need to tell somehow your shell or Jenkins about it. If import pandas gives you an error then you're probably using a different interpreter or a different python environment (but this is out of scope here).
Coming back to your script
Following this stack overflow answer, you'll see that all the line #!/usr/bin/env python does, is to make sure that you're using the first python interpreter on your Ubuntu's environment path. Which almost for sure isn't the one you installed with anaconda3. Most likely it will be the default python 2 distributed with ubuntu. If you want to make sure which interpreter exactly is running your script, instead of 'Hello World' put inside:
#!/usr/bin/env python
import sys
print(sys.executable) # this line will give you the exact path to the interpreter
print(sys.version) # this one will give you the version
Ok, so what to do?
Well, run your script using the correct interpreter. Remove #!/usr/bin/env python from your file and if you have a pipeline, add there:
sh "/home/yourname/anaconda3/bin/python /path_to_python_file/myFile.py"
It will most likely solve the issue. It's also quite flexible in the sense that if you ever want to use this python file on a different machine, you won't have your username hardcoded inside.

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.

Missing numpy dependency

I'm trying to build a python script that is suppose to feed into another Matlab program. the script uses (among other things) numpy and pandas.
Here's the matlab code when I try to load the script:
path='C:\XXXXXX\Local\Continuum\anaconda3\python.exe';
pyversion(path)
algo=py.importlib.import_module('Algo_Pres');
When I try to load the script into matlab, I get an import error that seems to originate from python:
I understand the error as: pandas is missing a numpy dependency.
And yet when I turn back to python and run the script in python it works smoothly...
Where do you think the problem comes from?
PS: I checked my Library using conda list in the Prompt.
For some reason numpy is listed in the anaconda channel, whereas anything else is listed without any channel. Do you think it could be linked?

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.

Python: Can't Find Module When Running Script Within a Script

I created a Python program A for which I later wrote another Python program B that called A using the subprocess library. I had a module foo installed in A which worked perfectly fine until I started calling the program from B. Now, I get the error:
ImportError: no module named foo
when B is called. I am sure that the module is installed correctly, because when I enter a python shell from the same directory as A and B, I am able to import foo and use its functions successfully. So, why wouldn't foo import correctly in this situation?
EDIT
I call program B from program A using the following call.
call(["python", "levMap9.py", inputFilePath, outputFilePath, scalingFactor])
In program B (levMap9.py), I make the following import, which gives the import error for some reason
import Levenshtein as LV
EDIT2
I realize that it is probably worth mentioning that these programs worked fine when I initially developed them on OSX, but now are having this problem on a Windows 8 machine.
I tried adding the path to the Levenshtein module in PYTHONPATH (it already existed in PATH), and that solved my problem; though I don't entirely understand why. Thanks to those who contributed advice.
EDIT
Found the real answer to my problem here: How to execute Python scripts in Windows?

Categories