Vector3 in Meep in Python - python

I'm getting 'module' object has no attribute 'Vector3' error in my simple python code.
This is rightAngle.py file
import meep as mp
import math
cell = mp.Vector3(16, 8, 0)
geometry = [mp.Bloack(mp.Vector3(1e20, 1, 1e20),
center = mp.Vector3(0, 0),
material = mp.Medium(epsilon = 12))]
sources = [mp.Source(mp.ContinuousSource(frequency = 0.15),
component = mp.Ez,
center = mp.Vector3(-7, 0))]
pml_layers = [mp.PML(1.0)]
resolution = 10
Compling using:
python rightAngle.py >& rightAngle.out
And getting this output:
Traceback (most recent call last):
File "rightAngle.py", line 4, in <module>
cell = mp.Vector3(16, 8, 0)
AttributeError: 'module' object has no attribute 'Vector3'
All I'm doing is copy and pasting given code from this fairly reliable source but getting error in Vector3 object.
Python version:
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
I don't know what I'm missing?
Meep Documentation:
https://meep.readthedocs.io/en/latest/Python_Tutorials/Basics/

Got the same problem while trying to use it on Ubuntu 16.04.
What worked for me was installing Anaconda for python 2.7 and create their recommended environment using:
conda create -n mp -c chogan -c defaults -c conda-forge pymeep
No Vector3 problem after that.

My environment script is below but I won't go into great detail here. My plan is to create a git repository based on my current working setup and make that public on Github which will force me to organize the whole setup and make it useful for others.
The following script allows me to run Meep with python3 without Anaconda3. This way you can use the latest packages and git source to build Meep, Open MPI and supporting libraries. My home directory is replaced by
#!/bin/bash
export MPI_PROJ=/home/<USER>/projects/mpi
export MPI_BASE=/usr/local/mpi/openmpi/base
export MPI_411=$MPI_BASE/411
#
# CRITICAL ENVARS - THESE MUST BE SET AS FOLLOWS FOR THE EXISTING MEEP, MPB AND OPENMPI 4.1.1
#
export LD_LIBRARY_PATH="/usr/local/mpi/meep/latest/lib:/usr/local/mpi/openmpi/base/411/bin:/home/fraser/.local/bin:/usr/local/bin:/usr/bin:/opt/bin:/bin:/sbin:/home/fraser/projects/mpi/bin"
export CFLAGS="-I/usr/local/mpi/meep/latest/include -I/usr/local/mpi/mpb/latest/include -I/usr/include/python3.8"
export PKG_CONFIG_PATH="/usr/local/mpi/meep/latest/lib/pkgconfig:/usr/local/mpi/openmpi/base/411/lib/pkgconfig:/usr/lib/x86_64-linux-gnu/pkgconfig:/usr/local/mpi/meep/latest/lib/pkgconfig"
export LDFLAGS="-L/usr/local/mpi/openmpi/base/411/lib -L/usr/local/mpi/meep/latest/lib -lmpi -lmpb"
export PYTHONPATH="/home/fraser/.local/lib/python3.8/site-packages:/usr/lib/python3.8:/usr/local/mpi/meep/latest/lib/python3.8/site-packages:/usr/local/lib/python3.8/dist-packages"
export PATH=/home/fraser/projects/mpi/bin:/usr/local/mpi/meep/latest/bin:/usr/local/mpi/openmpi/base/411/bin:/home/fraser/.local/bin:/usr/local/bin:/usr/bin:/opt/bin:/bin:/sbin
Open MPI 4.1.1 was built many times first to get it right on 3 real machines and 2 virtual machines. In the end all 3 Centos 8 Stream installations ran mpi flawlessly together. Two ubuntu 21 machines (one real, one vm) were able to run mpi together flawlessly. Centos 8 machines would talk to Ubuntu 21 machines. I could not copy the mpi installation between Ubuntu 21 and Centos 8 Stream due to glibc version differences.
Then moved on to Meep which required all the libraries listed in their "Building Meep From Source" documentation page. Some were not available from Mint 20 repos and had to be built from source. Build scripts were created to run configure for each one in order to record the options used.
Meep required specific builds of harminv, fftw3, guile, swig, hdf5. Other libraries were installed from the mint 20 package repositories using apt.
Meep was also built multiple times because my envrionment was to set right to run Meep from a location other than /usr/local. It took some tinkering to get the above environment variables close enough. They may need some tweaking for your purposes.
An intermediate update - I was able to duplicate my success on Mint20 Linux on Centos 8 Stream. Will gather all the working parts into a git repo and document the little details that nobody seems to post on here on StackOverflow or even on the Github issues! For example, export PY3=1 BEFORE RUNNING AUTOGEN in the swig git clone. Otherwise it wants to use python 2!
Another issue is: Save your clone as git-meep for example. Then copy that to meep. Then work on meep. If you use configure/make/make test/make install more than 5 times or any code or environment changes don't get noticed delete the meep tree. Make sure you save any environment-setting or configure-running scripts before deleting.
Copy the git-meep to meep and run autogen.sh again before running any environment-setting scripts. Then run those then run any scripts that run configure and/or make.
My configure scripts strictly run configure. Then I run make/make check/make install by hand, in case something fails to build.
There are more strange things to do that make this work.
Will post back here when the github repository is online.

Related

Getting MATLAB R2019b to use Python3.6

I am trying to use python 3.6 within the MATLAB interpreter. I have both Python 2.7 and 3.6 installed on my machine. When using the default python 2.7 MATLAB work fine:
>> pyenv('Version', '/usr/bin/python')
ans =
PythonEnvironment with properties:
Version: "2.7"
Executable: "/usr/bin/python"
Library: "libpython2.7.so.1.0"
Home: "/usr"
Status: NotLoaded
ExecutionMode: InProcess
>> py.sys.version
ans =
Python str with no properties.
2.7.5 (default, Aug 7 2019, 00:51:29)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
However when following MATLAB's directions to change versions of python by entering the path of the python 3 executable file, python will not load:
>> pyenv('Version', '/opt/rh/rh-python36/root/usr/bin/python3.6')
ans =
PythonEnvironment with properties:
Version: "3.6"
Executable: "/opt/rh/rh-python36/root/usr/bin/python3.6"
Library: ""
Home: "/opt/rh/rh-python36/root/usr"
Status: NotLoaded
ExecutionMode: InProcess
>> py.sys.version
Unable to resolve the name py.sys.version.
I do notice that the library field in pyenv is not filled in for python 3. So it appears that MATLAB cannot find a python 3 shared object file. Why would MATLAB have problems finding the libpython shared object file?
For me installing libpython3.6 solved this problem, i.e. sudo apt install libpython3.6 on my system. Now the Library field in pyenv is populated and I am able to run python commands.
First of all leave python version installation over to anaconda when it comes to matlab.. and work from within python with your matlab scripts. The next steps to get your python/matlab combo (or visa versa) up and running:
conda install matlab_engine
import matlab.engine
If you hit the bumper:
PackageNotFoundError # (it doesn't install).
Open the Anaconda prompt (be sure you are in desired python version environment (e.g. not base but matpy37):
a) cd matlabroot\extern\engines\python or e.g. cd /Applications/MATLAB_R2019a.app/extern/engines/python on MacOS. (# change to your matlab version if needed).
b.1) and type and run python setup.py install or in some cases bullet b2).
b.2) python setup.py install --prefix "<anaconda installation directory>".
If you don't have for example the proposed conda matpy37 test environment you can create it as followed:
i. conda create -n myenv python=3.6.9 # where e.g. myenv = matpy37. When install done then:
ii. type conda activate matpy37 and run from e.g. Ipython your matlab script by starting it form Jupyter Notebook (Windows: start -> find folder anaconda -> icon Jupyter Notebook (matpy37). Not sure where they hide it in your linux/ubuntu OS.
... in the worse case scenario there are two options (4 and 5):
Don't install the python versions via pip but via anaconda if you want to stay away from breaking package dependencies, fixing callbacks, loggers, diving into subprocess errors and exhausting manual updates and/or down-grades due to package incompatabilities, and more...
Why anaconda:
You can save your specific environment and its package version into a yml-file and recreate it on another PC/Laptop/OS system. The .ylm is in most cases less than 1Mb. So it fits on an usb-stick (some of us call it.. a thumb-drive ;-).
... and possible a last resort (never had to use this myself):
matlab package condalab. Its used for easy switch within matlab.

Issue with two versions of python

Problem having two versions of python on my Mac and them interacting. I have the following python locations
python is /anaconda3/envs/fenicsproject/bin/python
python is /usr/bin/python
and when I try to run a script. I get the following error:
Fatal Python error: PyThreadState_Get: no current thread
Abort trap: 6
I have googled some solutions and have found some posts saying I should try
env PYTHON_CONFIGURE_OPTS="--enable-framework" #or
env PYTHON_CONFIGURE_OPTS="--enable-shared"
This is they type of code I try to run
#Import packages
import dolfin as dl
I installed the env fenics by following the directions here
The google search possibly found Homebrew + Python on mac os x 10.8: Fatal Python error: PyThreadState_Get: no current thread importing mapnik however I was unable to find the library that links to a wrong version of python using otool.
I also found https://github.com/enthought/mayavi/issues/552 which suggests pinning to a different version of python.
Based on the install notes for hippy, https://hippylib.readthedocs.io/en/latest/installation.html then where they say conda create -n fenicproject ... you need to substitute the following:
conda create -n fenicsproject python==3.5.1
conda install -n fenicsproject -c conda-forge fenics==2017.2.0 \
mpi4py matplotlib scipy sympy==1.1.1 jupyter
After this, python -c 'import dolfin' no longer fails. It might also be possible to use a later version of python (I only tried 3.5.1).
What OS are you using? That will largely determine the specifics of how to go about correcting this issue, but the key here is which Python version the system path points to and what Python version your IDE is pointing to.
What you largely want to avoid is a situation where you are running Python scripts via your native system Python (2, likely).
Check which version of Python your IDE is using (from: How do I check what version of Python is running my script?):
import sys
print(sys.version)
Is the first number a 2? Did you want to use Python 2?
Next, let's check what version your system currently defaults to. If Ubuntu/Linux, use:
python -V
Is this expected? If not, you may need to change your system environmental variables to point to the correct Python version. The solution to this is OS dependent. If Windows, search "Edit Environmental Variables for Your Account" -> "Environmental Variables" -> "Path" , be sure it either points to Anaconda or the correct Python version; if Ubuntu/Linux, check your .bashrc file:
gedit ~/.bashrc
to see if the system points to the correct Python variable. If using a Mac, I formally apologize.

How do I install python on a windows machine using a cygwin terminal?

I am trying to install python through cygwin on a windows machine in order to make use of a statistical program that can only be implemented using python in a linux environment.
I have run setup-x86_64.exe and installed all python related cygwin packages in an attempt to install python, but when I open a terminal and type python I just get
$ python
-bash: python: command not found
which is the same for all python commands I try so clearly the installation has not occurred. However, I have found a path to Python-2.7.9 which appears to contain the files required to build python from source.
When I run ./configure it is successful, but when I then input make it fails due to:
File "build/temp.cygwin-1.7.35-x86_64-2.7/libffi/fficonfig.py", line 33, in <module>
ffi_sources += ffi_platforms['X86_WIN64']
KeyError: 'X86_WIN64'
Makefile:488: recipe for target 'sharedmods' failed
make: *** [sharedmods] Error 1
Anyone know why make won't work?
The options in ./configure --help are unfamiliar to me, could any of these options allow for successful installation?
Any ideas would be greatly appreciated, thank you.
Cygwin comes with an installer, called setup.exe or setup-x86_64.exe. Just run it and type python to the search box. Then let it install it for you.
python language interpreter is the basic package you need.
So, this question is 3 years old, but this error still happens with most recent versions of Python 2 (tested with 2.7.13 and 2.7.15)
I was able to get python to build by modifying the following file in the build directory.
--- build/temp.cygwin-2.11.1-x86_64-2.7/libffi/fficonfig.py~ 2018-11-03 14:51:53.290000000 -0400
+++ build/temp.cygwin-2.11.1-x86_64-2.7/libffi/fficonfig.py 2018-11-03 14:53:41.793000000 -0400
## -9,6 +9,7 ##
'X86': ['src/x86/ffi.c', 'src/x86/sysv.S', 'src/x86/win32.S'],
'X86_FREEBSD': ['src/x86/ffi.c', 'src/x86/freebsd.S'],
'X86_WIN32': ['src/x86/ffi.c', 'src/x86/win32.S'],
+ 'X86_WIN64': ['src/x86/ffi.c', 'src/x86/win64.S'],
'SPARC': ['src/sparc/ffi.c', 'src/sparc/v8.S', 'src/sparc/v9.S'],
'ALPHA': ['src/alpha/ffi.c', 'src/alpha/osf.S'],
'IA64': ['src/ia64/ffi.c', 'src/ia64/unix.S'],
The source files, in particular src/x86/win64.S, are present in the source tree. For some reason, ./configure does not seem to be properly adding the option to the dictionary in the fficonfig.py file, which causes the KeyError posted in the question. After making this modification, make and make install completed successfully and I was able to install an alternate version of python in addition to the normal Cygwin package.
I'm not yet sure why this happens, but I thought I would attempt to provide a solution to the question actually asked rather than just telling the asker to install a pre-compiled version using the package manager.
Since building Python from source was said to be a bad idea, I assumed my Cygwin was broken. I attempted to uninstall Cygwin following the instructions on the Cygwin website, but I was unable to remove it due to permission difficulties (I use a university-supplied Windows machine). My last resort was to run the Cygwin installation wizard setup-x86_64.exe and uninstall all packages, then run the wizard once more and select the necessary packages again as if starting from new. Miraculously, installing the required packages from scratch solved the problem and I am now running Python through my Cygwin terminal with no errors.
If you want to run Python on Cygwin, you might need to build it from source.
download the source
unpack it to the directory you could delete later, eg: build
run Cygwin, type: cd /path/to/build
run ./configure --help and read the help about the options you'll need
./configure #options
make

How do I build pysvn on Windows32?

I'm trying to build PySVN from source on my Windows 7 PC. It's running 64bit Windows, but for various reasons I need to compile it so that it works on 32bit Python. That's what we run on a lot of our automation servers.
I've downloaded the PySvn extensions source, I've got Visual Studio 2008 Express Edition installed. I've tried to a batch to automate the process, it looks like this:
set PROJECT_DIR=%~dp0
set SRC_DIR=%PROJECT_DIR%pysvn-1.7.8
cd %SRC_DIR%\Builder
set SVN_VER_MAJ_MIN=1.8
call builder_custom_init.cmd
cd %SRC_DIR%\Source
python setup.py configure --platform=win32
When I get to the last line I get the error message:
Info: Configure for python 2.7.6 in exec_prefix c:\python27
('Error:', 'cannot find PyCXX include CXX/Version.hxx - use --pycxx-dir')
My python - include directory does not contain a file called Version.hxx. Where do I get this file, how do I fix this bug?
One way is to install PYCXX by hand - it installs a Version.hxx under the Include folder on windows.
The source for PYCXX is here: http://cxx.sourceforge.net/
Another way is to point the C/L parameter --pycxx-dir at the Import folder under the pysvn root where the corresponding version of PYCXX for the version of pysvn is kept.
(On top of this you will have to build the subversion libraries on windows.)

JPype installation on Mountain Lion: "error: command 'gcc' failed with exit status 1" [duplicate]

I am trying to use Neo4j for a project, and want to interface with it through Python since I'm a newbie to programming and don't know any Java. I'm following the installation instructions, but I'm stuck on
the first step, which is to install JPype.
I'm using OS X 10.7 (lion). I think my configuration is pretty standard
with Python 2.7.2 downloaded from the Python website and Java 1.6.0 downloaded from the Apple website.
When I run
% sudo python setup.py install
On the JPype installer, I get about a 100 lines of error code about various .h files, then it
terminates with the lines:
lipo: can't figure out the architecture type of: /var/tmp//
ccwOzLi9.out
error: command 'gcc-4.2' failed with exit status 1
I found a blog post about a gcc error with JPype, but I followed the instructions there to no avail. I also emailed the author of that post, and he told me had never actually used JPype, had been working in OS X 10.6, and didn't have any insight.
I also emailed the creator of JPype, who told me that he only uses Windows, and has no idea how to make the install work on OS X. But if we can solve this, I can point him to the answer and maybe he can add the solution to the JPype documentation and help lots of other people as well!
So, anyone know what I'm doing wrong? I would like to use Neo4j, but I
don't know Java so I'm at a complete loss for how to fix a compiler
error.
Based on reading every Google result available, my two running
theories are that:
I'm somehow using a 32-bit version of Python or of Java (though I
used standard official installations and can't figure out how to
switch to 64-bit or if that's even possible)
The JPype files can only be compiled using GCC 4.0 instead of 4.2.
But I can't find anything online about how to rollback to GCC 4.0 (or
if it comes shipped with 2011 MacBooks and there is some way to force
JPype to compile with that instead).
There is another similar question, but the solution there is to use a different adapter that goes through REST instead of hooking directly into Java. I will try that if I have to, but I would really rather use the recommended Neo4j method if it's possible.
I'm not a Python guy, but tried installing JPype on my machine:
% uname -a
Darwin fatty-i7.local.tld 11.2.0 Darwin Kernel Version 11.2.0: Tue Aug 9 20:54:00 PDT 2011; root:xnu-1699.24.8~1/RELEASE_X86_64 x86_64
% java -version
java version "1.6.0_29"
Java(TM) SE Runtime Environment (build 1.6.0_29-b11-402-11M3527)
Java HotSpot(TM) 64-Bit Server VM (build 20.4-b02-402, mixed mode)
On OSX Lion, the latest JDK appears to be located here:
/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/JavaVM.framework/
A little googling turned up this post: http://blog.y3xz.com/post/5037243230/installing-jpype-on-mac-os-x
I followed those instructions to modify setup.py, then ran sudo python setup.py install with no problems.
Does that help?
On my Lion, the "include" directory in the JDK was gone, so JPype couldn't find jni.h.
I updated setup.py in two places, one to set where to find jar libraries:
def setupMacOSX(self):
self.javaHome = '/System/Library/Frameworks/JavaVM.framework'
self.jdkInclude = ""
self.libraries = ["dl"]
self.libraryDir = [self.javaHome+"/Home/lib"]
self.macros = [('MACOSX',1)]
And one to set where to find jni.h:
def setupInclusion(self):
self.includeDirs = [
self.javaHome+"/Headers",
<other stuff>
For me,
self.javaHome = '/System/Library/Frameworks/JavaVM.framework/Versions/Current/'
worked.
$ uname -a Darwin 11.4.0 Darwin Kernel Version 11.4.0: Mon Apr 9 19:32:15 PDT 2012; root:xnu-1699.26.8~1/RELEASE_X86_64 x86_64
$ java -version java version "1.6.0_33" Java(TM) SE Runtime Environment (build 1.6.0_33-b03-424-11M3720) Java HotSpot(TM) 64-Bit Server VM (build 20.8-b03-424, mixed mode)
Here are directions I put in my README to install JPype on OS X 10.7. Same idea as the answers here but different enough to warrant the submission.
The python interface to java (JPype) needs mods to the setup.py:
In summary, you need to make sure the JPype setup.py script can see your Java SDK "Headers" and "Home" directory
I had to install Java from Apple first since my default OS X installation did not come with Headers in the typical Java install path which I found by executing:
/usr/libexec/java_home
If you do happen to have a Headers dir, you probably do not need to reinstall Java and can set the path vars below based on your java HOME directory which is likely different from those in these directions
After I installed Java, I found the new installation in:
/Library/Java/JavaVirtualMachines/
My Home directory was:
/Library/Java/JavaVirtualMachines/1.6.0_37-b06-434.jdk/Contents/Home/
and my Header directory was:
/Library/Java/JavaVirtualMachines/1.6.0_37-b06-434.jdk/Contents/Headers/
1) The JPype script assumes that Headers is within the Home directory but it's not, so I changed the Home path var and created a new Content path var in the setup.py script - In setupMacOSX(self):
self.javaHome = '/Library/Java/JavaVirtualMachines/1.6.0_37-b06-434.jdk/Contents/Home/'
self.javaContents = '/Library/Java/JavaVirtualMachines/1.6.0_37-b06-434.jdk/Contents/'
2) In setupMacOSX(self), change self.libraryDir:
self.libraryDir = [self.javaContents + "/Libraries"]
Note that this step was mentioned as required but I did not have to do this for it to work so maybe try without it first
3) In setupInclusion, add paths to your "Home/include" dir and your "Headers" dir:
self.javaHome+"/include",
self.javaContents + "/Headers",
4) Running the installation should now work:
sudo python setup.py install
For everyone who is still trying to install Jpype but has meanwhile updated Mac OS: Andreas Kolleger's answer just works fine, but with XCode 4.3 the path changed to /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/JavaVM.framework/Versions/Current/
In ML it's MacOSX10.8.sdk. At least on my system. :)
For me, on Mountain Lion
self.javaHome = '/System/Library/Frameworks/JavaVM.framework/'
worked.
$uname -a
Darwin 12.0.0 Darwin Kernel Version 12.0.0: Sun Jun 24 23:00:16 PDT 2012; root:xnu-2050.7.9~1/RELEASE_X86_64 x86_64
$ java -version
java version "1.6.0_33"
Java(TM) SE Runtime Environment (build 1.6.0_33-b03-424-11M3720)
Java HotSpot(TM) 64-Bit Server VM (build 20.8-b03-424, mixed mode)
I did the same, but choosing
self.javaHome = '/Developer/SDKs/**MacOSX10.6.sdk**/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/'
instead
self.javaHome = '/Developer/SDKs/**MacOSX10.7.sdk**/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/'
realize that I use version 6 vs 7. With the 7 version I got the same errors that the beginning.
To make Will's answer a bit more specific: I had to modify the setupInclusion(self) method by adding the directory path he suggested, ie, '/System/Library/Frameworks/JavaVM.framework/Headers' on Mountain Lion (java version 1.6). With that the installation of JPype succeeded (though gave a bunch of warnings...)
Here's what worked for me - recommend you put the first line in your profile.
export JAVA_HOME=$(/usr/libexec/java_home)
cd $JAVA_HOME
sudo ln -s include Headers
sudo cp include/darwin/* include/
cd -
Note that instead of changing setup.py, I'm changing my JDK install. This has the advantage of fixing the issue for other projects.
For those trying to install on Mountain Lion, I had to further edit the setup.py file to include the header files here:
/System/Library/Frameworks/JavaVM.framework/Versions/Current/Headers

Categories