Importing numpy package into ABAQUS 6.10 Python environment - python

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.

Related

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

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.

Trying to import python modules returns ModuleNotFound Error

Im attempting to use the numpy module in webots but whatever I try I always get
INFO: assignment_2_controller_2: Starting controller: python3.6 -u "assignment_2_controller_2.py"
[assignment_2_controller_2] Traceback (most recent call last):
[assignment_2_controller_2] File "assignment_2_controller_2.py", line 4, in <module>
[assignment_2_controller_2] import numpy
[assignment_2_controller_2] ModuleNotFoundError: No module named 'numpy'
WARNING: 'assignment_2_controller_2' controller exited with status: 1.
I have installed numpy with both pip2 and pip3 and neither seem to have made any difference and the package shows up in ~/.local/lib/python3.6/site-packages. I can also confirm that this module works in the python editor and in other python files.
Is there perhaps something I need to change in webots python Makefile or perhaps in my environment variables?
Edit:
Printing sys.path shows
'/snap/webots/14/usr/share/webots/lib/controller/python36', '/snap/webots/14/usr/lib/python36.zip', '/snap/webots/14/usr/lib/python3.6', '/snap/webots/14/usr/lib/python3.6/lib-dynload', '/snap/webots/14/usr/lib/python3/dist-packages'
I installed webots using snap but why wouldnt it use the python dictated by the PATH environment variable?
Fix:
By the looks of it snap creates a contained filesystem for webots to run in by design in which it installed its own python binaries. I therefore uninstalled webots with snap and then installed from the .deb file off their website and now it works fine.
Webots installed from the Snap package is running in a kind of sandbox, it is therefore using the version of Python distributed with the snap package (which you can't alter).
As explained in the documentation, you might use external controllers to launch yourself the controller instead of letting Webots starting it. This way you can chose which version of Python you want to use: https://cyberbotics.com/doc/guide/installation-procedure#installing-the-snap-package
Alternatively, you can use a different way to install Webots (from the Debian or archive package). The other installations are not coming with Python and are using the system Python.

ImportError: No module named gdal

My GDAL is showing strange behaviour when I try to use ".py" functions from the command line:
1. For example, when I run gdalinfo --version, I get the standard response:
GDAL 1.11.3, released 2015/09/16
2. Also when I run gdalwarp, I get the standard response showing me the flags and everything.
3. Problem:
However, when I run gdal functions with a .py extension, for example gdal_polygonize.py, I get the following:
Traceback <most recent call last>:
File "C:\OSGeoW64\bin\gdal_polygonize.py", line 36, in <module>
import gdal, ogr, osr
ImportError: No module named gdal
I checked my Path variable, reinstalled python and gdal, but nothing worked.
When I run the gdal_polygonize.py function from my QGIS it works. I just can`t use it from my command line.
There are two parts to GDAL: the GDAL utilities (gdalinfo, gdalwarp, etc.) and the GDAL Python Bindings (when you call from osgeo import gdal from within a Python script.). The two can be (or could be in the past) installed separately.
The fact that you "see" gdalwarp on the command line means that you have the location of the utilities on your PATH (environment variables). Some of the utilities are self-contained, some require the Python bindings (notably those that require calling a .py file). In order for the Python bindings to work, GDAL has to be on the PYTHONPATH environment variable.
Find the GDAL folder (will contain gdalwarp, for instance). Find the osgeo folder in Python's Lib/site-packages. Add both of these locations to PATH and PYTHONPATH. You should now be able to from osgeo import gdal in a fresh Python shell.
If you cannot, either you are missing some files in those locations (bad install), or you have not specified the path correctly / set the right environment variables. There's no other magic involved, it's either installed and linked correctly, or you did something wrong.
Note that for environment variables to be set and working, you need to "Apply/OK" the environement variables window(s), and also start a fresh command prompt / Python shell.
make sure you install the conda version of gdal. I never really got gdal to work properly until I installed it from conda. Make sure you have the anaconda python distribution. https://www.continuum.io/downloads

Conflicting versions of python in ubuntu

So i had python 2.7.2 on my server and i needed to update it to python 2.7.3. So i've tried to remove the 2.7.2 version and then install the new one using the sources. I wasn't able to remove the 2.7.2 version cause the system uses it to run crucial services on server, so i installed the 2.7.3 version in hope that after that i would be able to remove the old version. Still i cant remove the old version, although i'm able to execute the python 2.7.3 when i install any module i cant import it. I added the path to sys.path and i started finding the module but importing it causes another errors.
My python executes the /usr/local/bin/python which is the 2.7.3 version where the problems are.
If i try to execute python like this /usr/bin/python it executes the old version and everything works fine there, i can import the new installed modules.
So what can i do to make python 2.7.3 work?
I've searched a lot of tutorials and tried things like add the library in .pth files on python and i started finding the modules but when importing it i get errors like this:
>>> import numpy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/numpy/__init__.py", line 137, in <module>
import add_newdocs
File "/usr/local/lib/python2.7/dist-packages/numpy/add_newdocs.py", line 9, in <module>
from numpy.lib import add_newdoc
File "/usr/local/lib/python2.7/dist-packages/numpy/lib/__init__.py", line 4, in <module>
from type_check import *
File "/usr/local/lib/python2.7/dist-packages/numpy/lib/type_check.py", line 8, in <module>
import numpy.core.numeric as _nx
File "/usr/local/lib/python2.7/dist-packages/numpy/core/__init__.py", line 5, in <module>
import multiarray
ImportError: /usr/local/lib/python2.7/dist-packages/numpy/core/multiarray.so: undefined symbol: PyUnicodeUCS4_AsUnicodeEscapeString
Thanks for the help
EDIT PROBLEM SOLvED
So to solve the missing import modules i created a .pth file under /usr/local/lib/python2.7/site-packages/ with the directories where the python modules are and the python starts to find them.
To fix the comptability problems you can install python from sources and specify the unicode doing ./configure --enable-unicode
more information here
Do not EVER mess with system python, EVER.
What you should do is install python 2.7.3 with a --prefix into your home directory, then use virtualenv -p /home/myuser/path/to/python.
In any case, using virtualenv to run your own application is almost always a good idea, as it avoids polluting the system package directories with libraries you use in your own applications.
It looks like the modules you've installed were built against your old version of Python, or at least a version incompatible with your newer installation. The import error you're seeing at the bottom is the numpy module searching for a symbol that is not in your build of 2.7.3. There is further information here.
If possible, it's usually way easier to upgrade Python with a package manager. That way, if anything on your system depends on Python, but does not need exactly 2.7.2, then Python can be easily upgraded without disturbing anything. I'm guessing that either your server doesn't have a newer version of Python available and you can't add new repositories, or you don't have access to a package manager. If using packages is possible, I would go ahead and remove what you've built from source (the command should be 'make clean' if Python uses GNU Make).
If that isn't an option, then there should be a way to compile Python, but not install it into system directories. Then you could add a symlink for users, and make sure that symlink has precedence in their path.
When installing python use the following steps
using prefix to specify the installation directory
./configure --prefix=/usr/bin/python
make
make install
Then everytime u run a new Terminal u have specify
export PATH="$PATH:/usr/bin/"
to tell where is the installation directory of Python
This way u can use any number of pythons
You can install python libs from R. It works for me.
For example, to install numpy library from R type:
system('python -m pip install -U numpy')

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