0

I am trying to run a python script using ASE library, I have installed ase (https://wiki.fysik.dtu.dk/ase/install.html)and run my python script using this library on my terminal. I have made a virtual env named 'MLC_env' and have already installed the ase library there.

There is a folder 'Desktop/MLC/MLC_env/lib/python3.10/site-packages/ase' which got installed over there. Now the the website (https://pypi.org/project/ase/) says Add ~/ase to your $PYTHONPATH environment variable and add ~/ase/bin to $PATH (assuming ~/ase is where your ASE folder is).

I am not able to figure out what exactly I have to do? How to add this ase folder to my pythonpath? Any suggestions will be helpful.

I tried to follow this answer Permanently add a directory to PYTHONPATH? , according to this I can try doing export PYTHONPATH="${PYTHONPATH}:/my/other/path"" where ~/ase will be the full path name of the ase folder. Shall I go ahead with this? any suggestions.

Python script

from ase import Atoms
atoms = Atoms('N2')
print(atoms.positions)

Error: While trying to run the above python script

(MLC_env) (base) anshumansinha@Anshumans-MacBook-Pro-3 Q1 % python3 struct.py
Traceback (most recent call last):
  File "/Users/anshumansinha/Desktop/MLC/MLC_env/lib/python3.10/site-packages/numpy/compat/py3k.py", line 24, in <module>
    import pickle5 as pickle
  File "/Users/anshumansinha/.local/lib/python3.10/site-packages/pickle5/__init__.py", line 1, in <module>
    from .pickle import *
  File "/Users/anshumansinha/.local/lib/python3.10/site-packages/pickle5/pickle.py", line 33, in <module>
    from struct import pack, unpack
  File "/Users/anshumansinha/Desktop/MLC/HW1/Q1/struct.py", line 1, in <module>
    from ase import Atoms
ImportError: cannot import name 'Atoms' from partially initialized module 'ase' (most likely due to a circular import) (/Users/anshumansinha/Desktop/MLC/MLC_env/lib/python3.10/site-packages/ase/__init__.py)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/anshumansinha/Desktop/MLC/MLC_env/lib/python3.10/site-packages/numpy/core/__init__.py", line 23, in <module>
    from . import multiarray
  File "/Users/anshumansinha/Desktop/MLC/MLC_env/lib/python3.10/site-packages/numpy/core/multiarray.py", line 10, in <module>
    from . import overrides
  File "/Users/anshumansinha/Desktop/MLC/MLC_env/lib/python3.10/site-packages/numpy/core/overrides.py", line 8, in <module>
    from numpy.compat._inspect import getargspec
  File "/Users/anshumansinha/Desktop/MLC/MLC_env/lib/python3.10/site-packages/numpy/compat/__init__.py", line 12, in <module>
    from . import py3k
  File "/Users/anshumansinha/Desktop/MLC/MLC_env/lib/python3.10/site-packages/numpy/compat/py3k.py", line 26, in <module>
    import pickle
  File "/Users/anshumansinha/miniforge3/lib/python3.10/pickle.py", line 33, in <module>
    from struct import pack, unpack
  File "/Users/anshumansinha/Desktop/MLC/HW1/Q1/struct.py", line 1, in <module>
    from ase import Atoms
ImportError: cannot import name 'Atoms' from partially initialized module 'ase' (most likely due to a circular import) (/Users/anshumansinha/Desktop/MLC/MLC_env/lib/python3.10/site-packages/ase/__init__.py)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/anshumansinha/Desktop/MLC/HW1/Q1/struct.py", line 1, in <module>
    from ase import Atoms
  File "/Users/anshumansinha/Desktop/MLC/MLC_env/lib/python3.10/site-packages/ase/__init__.py", line 17, in <module>
    from ase.atom import Atom
  File "/Users/anshumansinha/Desktop/MLC/MLC_env/lib/python3.10/site-packages/ase/atom.py", line 3, in <module>
    import numpy as np
  File "/Users/anshumansinha/Desktop/MLC/MLC_env/lib/python3.10/site-packages/numpy/__init__.py", line 141, in <module>
    from . import core
  File "/Users/anshumansinha/Desktop/MLC/MLC_env/lib/python3.10/site-packages/numpy/core/__init__.py", line 49, 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.10 from "/Users/anshumansinha/Desktop/MLC/MLC_env/bin/python3"
  * The NumPy version is: "1.24.1"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: cannot import name 'Atoms' from partially initialized module 'ase' (most likely due to a circular import) (/Users/anshumansinha/Desktop/MLC/MLC_env/lib/python3.10/site-packages/ase/__init__.py)
Formal_this
  • 121
  • 5
  • `~` is shell syntax that expands to the current value of `$HOME`. Inside double quotes, it isn't honored, so you want to use `$HOME` instead; thus, `~/ase` becomes `$HOME/ase` – Charles Duffy Feb 01 '23 at 01:19
  • Mind, `~ase` means "the home directory of the user named ase", whereas `~/ase` means "the directory named ase under the current user's home directory". Since they're not the same, be sure you know which of those two you want. – Charles Duffy Feb 01 '23 at 01:20
  • That said, if you already successfully "installed the ase library' in your virtualenv, you don't need to do any of this. (Those instructions look like they were written before `ase` _could_ be installed with `pip` or whatever other method you used, but if you've successfully installed it, you shouldn't need them). – Charles Duffy Feb 01 '23 at 01:22
  • But I am getting an error while running a python file, shall I include the error here in the question? – Formal_this Feb 01 '23 at 01:23
  • If you're really asking how to fix your error, not really asking how to set your PYTHONPATH variable, then yes, you need to actually show us the error. – Charles Duffy Feb 01 '23 at 01:23
  • (See how "Installation from source" and "Installation with pip" are two separate sections in the documentation? If you do the installation with pip, you don't need to do the installation from source; it's one or the other, not both). – Charles Duffy Feb 01 '23 at 01:24
  • Oh okay, I did installation with pip. – Formal_this Feb 01 '23 at 01:26
  • @CharlesDuffy I actually updated the question with all the details of the error. – Formal_this Feb 01 '23 at 01:28
  • Ah. Okay. There's the numpy problem, and the circular import problem; it's not obvious right now where the circular import problem comes from (I'd need to actually try to reproduce locally), but the numpy problem should be something you can fix on its own and see if the circular-import problem goes away with it. – Charles Duffy Feb 01 '23 at 01:28
  • And that numpy problem is one we have already asked and answered on this site already, so you should be able to find existing Q&A describing it. – Charles Duffy Feb 01 '23 at 01:29
  • Sure I'll try to get rid of the numpy problem and revert back asap. thanks a lot for the advice. – Formal_this Feb 01 '23 at 01:30
  • @CharlesDuffy I tried all the possible solutions listed on https://stackoverflow.com/questions/58868528/importing-the-numpy-c-extensions-failed , but non of them worked for me. – Formal_this Feb 01 '23 at 01:35
  • Do you still have that problem if you just run `import numpy` with no involvement of `ase` at all? – Charles Duffy Feb 01 '23 at 15:09
  • No, numpy works well otherwise. – Formal_this Feb 01 '23 at 15:13
  • Interesting; that does lean hard towards it being an ASE problem. If I have some time outside work hours I'll take a shot at reproducing this issue. The only thing I _might_ suggest trying in the interim is, if you don't expect `miniforge` to be involved in your Python environment, taking it out of the picture. – Charles Duffy Feb 01 '23 at 15:23
  • (that said, if I were installing ASE for my own personal use, I wouldn't be using pip at all, but would be installing it via [Nix](https://nixos.org/); one of the Big Ideas around Nix is that each package is isolated, read-only, immutable, and otherwise prevented from conflicting with other components on the system; granted, PYTHONPATH settings can mix things that have been more traditionally installed in, but a Nix environment is a lot more predictable and reproducible than the more traditional approach) – Charles Duffy Feb 01 '23 at 15:25
  • @CharlesDuffy Hey, thanks a lot for your help. I will work on these recommendations and update the post. And specially for taking your time out after work to help me. – Formal_this Feb 01 '23 at 15:27
  • Hey -- sorry I still haven't had a chance to look at this in full. But one thing I _was_ able to do even if just very quickly is to verify that telling Nix to provide `python310.withPackages (p: [p.ase])` results in a Python directory, with ASE installed, where the code in your question works correctly on MacOS (13.1, M1). – Charles Duffy Feb 02 '23 at 19:37

0 Answers0