Can't get python scripts to work in Power BI [duplicate] - python

This question already has answers here:
How to run Python Script on powerBI
(3 answers)
Closed last year.
I currently have anaconda installed on my machine. I have a python script written which is working perfectly in Jupyter Notebook, the next step is I want to execute this script in Power BI to import data and pre-process the data.
I have adjusted the settings in powerBI (correctly I think):
Python Directory is set to C:\ProgramData\Anaconda3
Detected Python IDEs is set to Default OS program for .PY files
Can you provide a solution for the below error. I have spent hours trying to find something.
Error
Details: "ADO.NET: Python script error.
<pi>C:\PROGRAMDATA\ANACONDA3\lib\site-packages\numpy\__init__.py:140: UserWarning: mkl-service package failed to import, therefore Intel(R) MKL initialization ensuring its correct out-of-the box operation under condition when Gnu OpenMP had already been loaded by Python process is not assured. Please install mkl-service package, see http://github.com/IntelPython/mkl-service
from . import _distributor_init
Traceback (most recent call last):
File "PythonScriptWrapper.PY", line 2, in <module>
import os, pandas, matplotlib
File "C:\PROGRAMDATA\ANACONDA3\lib\site-packages\pandas\__init__.py", line 16, in <module>
raise ImportError(
ImportError: Unable to import required dependencies:
numpy:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.
We have compiled some common reasons and troubleshooting tips at:
https://numpy.org/devdocs/user/troubleshooting-importerror.html
Please note and check the following:
* The Python version is: Python3.8 from "C:\PROGRAMDATA\ANACONDA3\python.exe"
* The NumPy version is: "1.18.5"
and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.
Original error was: DLL load failed while importing _multiarray_umath: The specified module could not be found.
</pi>"

If you need to run Python scripts in Power BI and have the Anaconda stack installed, it has to be activated before you can use it. Simply open the Anaconda Prompt or run conda activate on the cmd prompt. Then start PBIDesktop from within the activated environment like:
(base) C:\> "Program Files\Microsoft Power BI Desktop\bin\PBIDesktop.exe"
Make sure the Python home directory (Anaconda3) has been added to the PBIDesktop global options in the Python scripting section too.

Related

MAC Python 3.8 IntelliJ error while importing panda module

In my Apple M1 chip machine, trying to import panda module in IntelliJ IDEA. while running this program its giving below error.
If I run the same program in command line, it works. Running into this problem while running in the IDE.
Any help to resolve this error message?
Code
import os
import pandas as pd
Remove my home dir from this output:
Error
Traceback (most recent call last):
File "panda-test01.py", line 9, in <module>
import pandas as pd
File "Library/Python/3.8/lib/python/site-packages/pandas/__init__.py", line 16, in <module>
raise ImportError(
ImportError: Unable to import required dependencies:
numpy:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.
We have compiled some common reasons and troubleshooting tips at:
https://numpy.org/devdocs/user/troubleshooting-importerror.html
Please note and check the following:
* The Python version is: Python3.8 from "/Library/Developer/CommandLineTools/usr/bin/python3"
* The NumPy version is: "1.22.3"
and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.
Original error was: dlopen(Library/Python/3.8/lib/python/site-packages/numpy/core/_multiarray_umath.cpython-38-darwin.so, 0x0002): tried: 'Library/Python/3.8/lib/python/site-packages/numpy/core/_multiarray_umath.cpython-38-darwin.so' (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64'))
Thanks
from your error message:
(mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64'))
It's a little ambiguous as it could be interpreted either way round, but I think it means that IntelliJ is running using Rosetta2 in X86 mode, whereas the numpy/pandas installation the python PATH has is your arm version. (The alternative is the reverse of this, but this way round is more likely).
In order to run your program, you'll need to create a python environment which uses Rosetta2 and installs X86 binaries instead of arm. I've written about how to do this here as a reference for myself. Then IntelliJ should have a setting somewhere to specify the python environment to use for execution and debugging, you should set that environment/PATH to your new X86 environment.

How to use Python modules for interactive scripting in Valor NPI tool

I am trying to interact with a tool called Valor NPI using a Python script. This tool is used for designing and manufacturing electronic goods. As my first step, I ran a simple "hello world" program and it's running fine. But when I try to run some script using Python modules, it is showing a ModuleNotFound error. To resolve this problem, I've put the required modules and pip manually in that location (\Valor\vNPI_114\edir\all\python). Then, to check if it worked, I wrote a small script using the numpy module and tried to run it in that Valor tool. But it is showing the following error:
File "C:\MentorGraphics\Valor\vNPI_114\edir\all\python\numpy\core\__init__.py", line 22, in <module>
from . import multiarray
File "C:\MentorGraphics\Valor\vNPI_114\edir\all\python\numpy\core\multiarray.py", line 12, in <module>
from . import overrides
File "C:\MentorGraphics\Valor\vNPI_114\edir\all\python\numpy\core\overrides.py", line 7, in <module>
from numpy.core._multiarray_umath import (
ModuleNotFoundError: No module named 'numpy.core._multiarray_umath'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/MentorGraphics/Valor/vNPI_DIR/sys/scripts/numpy1.py", line 2, in <module>
import numpy as np
File "C:\MentorGraphics\Valor\vNPI_114\edir\all\python\numpy\__init__.py", line 150, in <module>
from . import core
File "C:\MentorGraphics\Valor\vNPI_114\edir\all\python\numpy\core\__init__.py", line 48, in <module>
raise ImportError(msg)
ImportError:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.
We have compiled some common reasons and troubleshooting tips at:
https://numpy.org/devdocs/user/troubleshooting-importerror.html
Please note and check the following:
* The Python version is: Python3.8 from "C:\MentorGraphics\Valor\vNPI_114\edir\nv\deps\Python\python.exe"
* The NumPy version is: "1.21.4"
and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.
Original error was: No module named 'numpy.core._multiarray_umath'
How can I solve this? How can I use Python modules in that tool?
Python packages are installed in different ways, usually using pip, sometimes using a setup.py, but generally not by just coping a module to your Python directory. In most cases, that won't work.
I have no experience with the Valor NPI tool, and I recommend first searching for documentation of that tool if it supports third-party Python modules. Maybe there is a recommended way of installing.
Otherwise, I think you should install the needed modules using pip for this specific Python installation. In your case, I would execute:
C:\MentorGraphics\Valor\vNPI_114\edir\nv\deps\Python\python.exe -m pip install numpy
And likewise for other packages that you need (replacing numpy by the corresponding module). Since you already copied numpy to your Python directory, you should remove that to avoid conflicts between the different copies.
It looks like it could not find _multiarray_umath.cp310-win_amd64.pyd under your numpy/core folder.
Like #wovano, I suggest you install the module through pip instead of copying the module folder. Sometimes it can work, sometimes will not.
You can follow these steps:
In the terminal create a virtual environment through the command of
python -m venv .venv
use the Python: Select Interpreter command from the Command Palette
(Ctrl+Shift+P) to select the virtual environment .venv
Create a new terminal(Ctrl+Shift+`) that will activate the .venv virtual environment automatically
install the NumPy with the command of pip install numpy
You can refer to this official doc for the detailed steps.

How to solve pandas import error in pycharm?

I've already install pandas from either the terminal and add pandas in pycharm project interpreter. However, every time I run a program which uses pandas it keeps reminding me there's an error.
RuntimeError: module compiled against API version 0xb but this version of numpy is 0xa
Traceback (most recent call last):
File "/Users/Rabbit/PycharmProjects/NLP/review2vector.py", line 7, in <module>
from pandas import DataFrame
File "/Users/Rabbit/Library/Python/2.7/lib/python/site-packages/pandas/__init__.py", line 35, in <module>
"the C extensions first.".format(module))
ImportError: C extension: numpy.core.multiarray failed to import not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --inplace --force' to build the C extensions first.
I also followed this question's answer How to solve import error for pandas? But it does not work for me.
These issues can be easily avoided if you use a virtual environment to install and maintain your Python packages. Please refer to the link here for more information: LINK
The error message is telling you that numpy is not fully installed. There isn't enough information there to guess specifically what is wrong, but if I was troubleshooting I would use my package manager (pip probably) to uninstall and then re-install numpy and pandas. I would do numpy separately so that I could watch the messages. The numpy page says that they should have pre-compiled wheels available, so it just seems like a version mismatch.
Pycharm lets you install packages into a virtualenv easily and ensure that env is always activated when you open the pycharm terminal (great!) but it also makes it very hard to notice install errors.

Importing numpy package into ABAQUS 6.10 Python environment

I have not been successful at importing numpy package to ABAQUS Python environment from previous discussions on this similar problem.
I am working on Microsoft Windows 7 (64-bit). I have installed ABAQUS 6.10. And by typing the command abaqus python in MS-DOS prompt I figured that ABAQUS 6.10 uses Python 2.6.2 version. For an important reason, my python script file needs to use the array function available in the numpy package. I undertook the following steps based on previous discussions:
Python 2.6.2 installation:
Installed Python 2.6.2 version (32-bit) to C:\Python26\ (I already had Python 2.7.6 installed in folder C:\Python27\)
Changed the environment system variables under PATH variable to C:\Python26\; (previously it was C:\Python27\)
Changed the environment user variables under PYTHONPATH variable to C:\Python26\Lib\site-packages\ (previously it was C:\Python27\)
Loaded Python 2.6.2 successfully using command python in MS-DOS prompt.
Numpy installation for Python 2.6.2:
Downloaded and installed numpy (32-bit) MSI installer file numpy-1.3.0-win32-superpack-python2.6 from Sourceforge.net- NumPy 1.3.0 in C:\Python26\Lib\site-packages folder.
Loaded Python 2.6.2 successfully using command python in MS-DOS prompt.
Imported numpy successfully using the command import numpy in python environment.
Numpy installation for ABAQUS 6.10:
Copied the numpy folder from C:\Python26\Lib\site-packages\ and pasted to D:\SIMULIA\Abaqus\6.10-1\Python\Lib\site-packages, which is the python modules folder for ABAQUS. Please note I have installed ABAQUS 6.10 in D: drive.
Created a python script file, myScript.py, containing just the following line of code: from numpy import * and saved it in the desktop.
Opened MS-DOS prompt and changed folder destination to my desktop containing myScript.py.
I finally typed the following commands in MS-DOS prompt to run the script file in the ABAQUS Python environment: abaqus cae noGUI=myScript.py and I got the following errors:
Abaqus License Manager checked out the following license(s):
"cae" release 6.10 from 127.0.0.1
<2010 out of 2011 licenses remain available>.
ImportError: DLL load failed: %1 is not a valid Win32 application.
File "myScript.py", line 4, in <module> from numpy import array
File "C:\Python26\Lib\site-packages\numpy\__init__.py", line 130, in <module>
import add_newdocs
File "C:\Python26\Lib\site-packages\numpy\add_newdocs.py", line 9, in <module>
from lib import add_newdoc
File "C:\Python26\Lib\site-packages\numpy\lib\__init__.py", line 4, in <module>
from type_check import *
File "C:\Python26\Lib\site-packages\numpy\lib\type_check.py", line 8, in <module>
import numpy.core.numeric as _nx
File "C:\Python26\Lib\site-packages\numpy\core\__init__.py", line 5, in <module>
import multiarray
Abaqus Error: cae exited with an error.
Note: I installed Python 2.6.2 32-bit because the numpy available for 64-bit was showing a warning that it is unstable and would subsequently crash.
My basic question is: "How can I successfully import numpy package into ABAQUS environment?"
Honestly, the 32-bit version of numpy will not work with a 64-bit version of Python. Therefore, the error is quite expected. Even if Abaqus were to use a 32-bit version, I would strongly advise not to copy and paste the directory of the library.
dim_voly answer is an easy way to get around this problem, but here are some instructions if you really want to use numpy inside Abaqus
This link explains quite clearly how to install numpy. You will need to include the Abaqus Python directory in the PATH environmental variable. Once pip is installed, it should be easy to install numpy and any other library you need.
I am currently using Abaqus 6.13 which already comes installed with numpy. Thus, upgrading your software can also be a viable solution.
Hope this helps
I had trouble importing anything into the Abaqus python environment. The workaround I used was to generate the .py script using a main .py script. So all the calculations are done and pasted in as text into a template .py script. Then later the main script calls abaqus to run the python script.
I generally found that the python version within Abaqus is behind the mainstream python version and doing anything remotely out of the ordinary doesn't work as it depends on Simulia to update things. I use too many custom packages as well.
In any case you end up wanting a master python script to set up high level things like folders and reading in data from tables. And if you're running a script to build a model, I don't doubt that you're doing a parametric study so your loops for that can be in the main script.
What also sometimes works for passing data into the abaqus .py file is setting up a struct or class and saving it using pickle. As far as I recall pickle works and is importable as long as the class does not contain any custom imports.

How to use OpenCV in Python?

I have just installed OpenCV on my Windows 7 machine. As a result, I get a new directory:
C:\OpenCV2.2\Python2.7\Lib\site-packages
In this directory, I have two files: cv.lib and cv.pyd.
Then I try to use the opencv from Python. I do the following:
import sys
sys.path.append('C:\OpenCV2.2\Python2.7\Lib\site-packages')
import cv
As a result I get the following error message:
File "<stdin>", line 1, in <module>
ImportError: DLL load failed: The specified module could not be found.
What am I doing wrong?
ADDED
As it was recommended here, I have copied content of C:\OpenCV2.0\Python2.6\Lib\site-packages to the C:\Python26\Lib\site-packages. It did not help.
ADDED 2
My environment variables have the following values:
Path=C:\Program Files\MiKTex\miktex\bin;C:\OpenCV2.2\bin;C:\Python26;
PYTHONPATH=C:\OpenCV2.2\Python2.7\Lib\site-packages
Do I need to change something? Do I need to add something?
ADDED 3
I think my question is general: How to use a library? Probably I need to find a *.ddl file somewhere? Then I need to use the name of the directory containing this file as a value to some environment variables? Or maybe I need to use sys.addpath? I also need to know how the way to call the library is related to the name of the file that contains the library.
ADDED 4
It is interesting that when I type import cv, I get:
ImportError: DLL load failed: The specified module could not be found.
But when I type import opencv I get:
ImportError: No module named opencv
ADDED 5
It has been suggested that I usthe e inconsistent version of python. In more details, OpenCV tries to use Python2.7 and I had Python2.6. So, I have installed Python 2.7. It makes difference. Now I do not have the old error message, but I have a new one:
ImportError: numpy.core.multiarray failed to import
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: numpy.core.multiarray failed to import
ADDED 6
I have managed to resolve the problem by installing numpy. It took some time because I did not realized that there are different numpy installer corresponding to different versions of Python. Some details can be found in my answer to my own question (see bellow).
The problem was resolved. The following steps has been done:
A new version of python (version 2.7) has been installed.
After that I still was unable to run OpenCV because I had some problems with the numpy library.
I tired to install numpy but the installer did not see my new version of the Python.
I deleted the old version of Python as well as links to the old version in the Path system vatriable.
After that numpy installer was not able to finish the installation.
I have realized that I need to run another numpy installer that is associated with the Python 2.7. It can be found here.
Finally everything worked. I was able to "import cv".
I suspect you have the same problem I've run into. If you have a 64-bit version of Python, it cannot load 32-bit DLLs. OpenCV currently only ships 32-bit binaries. If you want 64-bit .pyd and .dll files, you have to compile them yourself. There are some instructions on the OpenCV Wiki, but it's not for the faint of heart. Expect to have a substantial time investment.
The easiest solution is to:
Uninstall 64-bit Python
Install a 32-bit distribution.
The PythonXY distribution includes pyopencv -- a good set of OpenCV hooks. The only limitation is that it's 32-bit, so don't make plans to process gigapixel astronomy data with it! ;)
If you must have the 64-bit version, follow these instructions to get it OpenCV to compile with Visual Studio 2010. There's a discussion on stackoverflow that describes building 64-bit apps with VC Express.
EDIT: OpenCV now ships with 64-bit Python binaries. The .dll files need to go somewhere in your path (I put them in the scripts folder), and the .pyd files go in your site-packages directory.
I had trouble interfacing OpenCV with Python, and I was looking all over the place for help. Here's what worked for me. I basically followed this post: http://opencvpython.blogspot.com/2012/05/install-opencv-in-windows-for-python.html. After downloading and extracting OpenCV 2.4.6, you basically get a folder called "opencv" with a bunch of stuff in it. Navigate to build->python->2.7. Inside, there is only one file called "cv2.pyd". I copied this file and pasted it in "python-2.7.5\Lib\site-packages". I'm actually using the Spyder IDE, and it works fine. In the python interpreter, typing in "import cv" worked for me.
Maybe you should edit your environment variable
right click on the "My Computer" or something like this, click on properties.
In the properties window click on the Advanced tab.
Then, the environment variables button.
Change the path.

Categories

Resources