Can't import numpy when venv is activated with activate_this - python

When I activate my venv normally, things work fine.
However, when I use the activate_this.py library to activate internally to my python script, I get:
>>> this_file='win\\CYAN\Scripts\\activate_this.py'
>>> exec(open(this_file).read(), {'__file__': this_file})
>>> import numpy
Traceback (most recent call last):
File "C:\Dropbox (CEP)\venvs\win\CYAN\Lib\site-packages\numpy\core\__init__.py", line 40, in <module>
from . import multiarray
File "C:\Dropbox (CEP)\venvs\win\CYAN\Lib\site-packages\numpy\core\multiarray.py", line 12, in <module>
from . import overrides
File "C:\Dropbox (CEP)\venvs\win\CYAN\Lib\site-packages\numpy\core\overrides.py", line 6, 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 "<stdin>", line 1, in <module>
File "C:\Dropbox (CEP)\venvs\win\CYAN\Lib\site-packages\numpy\__init__.py", line 142, in <module>
from . import core
File "C:\Dropbox (CEP)\venvs\win\CYAN\Lib\site-packages\numpy\core\__init__.py", line 71, in <module>
raise ImportError(msg)
ImportError:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
[LOTS of stuff about standard causes of this problem deleted ending with]
Original error was: No module named 'numpy.core._multiarray_umath'
In both cases I'm using the same python: C:\Python37\python.exe which is 64b (I ran virtualenv with -p python so the virtualenv is just a copy of that...)
ETA:
I see that there are some references from 2015 about numpy not being 64b... I assume that that's dealt with by now, because I've seen no reference to it since then... if I'm wrong, that would be good to know...
Added data: This appears to be something set up weirdly on MY computer.. when I try this on someone elses computer (set up supposedly the same way...) it works fine... Even across dropbox...
This probably has to do with the warnings I got when doing the pip install -r requirements.txt, but I'm not getting anymore so maybe there was a problem in the requirements.txt...

Related

Error in running python file using ASE library

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)

Not able to run Ludwig commands in terminal, and in Python also as it is saying no module installed named Ludwig

When is run "ludwig --version", it shows me the version of Ludwig installed without any errors, but when I try Ludwig train command it is showing me the error.
The error which I got after running "ludwig train --config rotten_tomatoes.yaml --dataset rotten_tomatoes.csv" is as follows:-
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/numpy/core/__init__.py", line 22, in <module>
from . import multiarray
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/numpy/core/multiarray.py", line 12, in <module>
from . import overrides
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/numpy/core/overrides.py", line 7, in <module>
from numpy.core._multiarray_umath import (
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/numpy/core/_multiarray_umath.cpython-310-darwin.so, 0x0002): tried: '/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/numpy/core/_multiarray_umath.cpython-310-darwin.so' (mach-o file, but is an incompatible architecture (have (arm64), need (x86_64)))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.10/bin/ludwig", line 5, in <module>
from ludwig.cli import main
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/ludwig/cli.py", line 19, in <module>
import ludwig.contrib
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/ludwig/contrib.py", line 18, in <module>
from .contribs import contrib_registry
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/ludwig/contribs/__init__.py", line 40, in <module>
from .mlflow import MlflowCallback
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/ludwig/contribs/mlflow/__init__.py", line 11, in <module>
from ludwig.utils.data_utils import chunk_dict, flatten_dict, save_json, to_json_dict
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/ludwig/utils/data_utils.py", line 34, in <module>
import numpy as np
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/numpy/__init__.py", line 150, in <module>
from . import core
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/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.10 from "/Library/Frameworks/Python.framework/Versions/3.10/bin/python3.10"
* The NumPy version is: "1.21.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/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/numpy/core/_multiarray_umath.cpython-310-darwin.so, 0x0002): tried: '/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/numpy/core/_multiarray_umath.cpython-310-darwin.so' (mach-o file, but is an incompatible architecture (have (arm64), need (x86_64)))
And when I run "from ludwig.api import LudwigModel", it doesn't work either.
Not able to figure out how to solve the error, please help.
Are you still running into this error with the latest Ludwig release, i.e., Ludwig 0.6.4? We fixed this in Ludwig 0.6 so you shouldn't see this anymore.

Problem launching Jupyter Notebook from Anaconda

I am having issues launching Jupyter Notebook. I have tried to do it two ways and seem to get errors.
1 - Launching from the Anaconda Prompt using the command jupyter notebook or directly choosing from the Anaconda menu
I get the following:
(base) C:\Users\metni>jupyter notebook
Traceback (most recent call last):
File "C:\Users\metni\anaconda3\lib\site-packages\jsonschema\__init__.py", line 31, in <module>
from importlib import metadata
ImportError: cannot import name 'metadata' from 'importlib' (C:\Users\metni\anaconda3\lib\importlib\__init__.py)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\metni\anaconda3\lib\site-packages\nbformat\validator.py", line 12, in <module>
from jsonschema import ValidationError
File "C:\Users\metni\anaconda3\lib\site-packages\jsonschema\__init__.py", line 33, in <module>
import importlib_metadata as metadata
ModuleNotFoundError: No module named 'importlib_metadata'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\metni\anaconda3\Scripts\jupyter-notebook-script.py", line 6, in <module>
from notebook.notebookapp import main
File "C:\Users\metni\anaconda3\lib\site-packages\notebook\notebookapp.py", line 80, in <module>
from .services.contents.manager import ContentsManager
File "C:\Users\metni\anaconda3\lib\site-packages\notebook\services\contents\manager.py", line 17, in <module>
from nbformat import sign, validate as validate_nb, ValidationError
File "C:\Users\metni\anaconda3\lib\site-packages\nbformat\__init__.py", line 32, in <module>
from .validator import validate, ValidationError
File "C:\Users\metni\anaconda3\lib\site-packages\nbformat\validator.py", line 23, in <module>
raise ImportError(str(e) + verbose_msg)
ImportError: No module named 'importlib_metadata'
Jupyter notebook format depends on the jsonschema package:
https://pypi.python.org/pypi/jsonschema
Please install it first.
2 - Launching from the command line with the same command
I get the following:
C:\Users\metni>jupyter notebook
Traceback (most recent call last):
File "C:\Users\metni\anaconda3\Scripts\jupyter-notebook-script.py", line 6, in <module>
from notebook.notebookapp import main
File "C:\Users\metni\anaconda3\lib\site-packages\notebook\notebookapp.py", line 64, in <module>
from tornado import httpserver
File "C:\Users\metni\anaconda3\lib\site-packages\tornado\httpserver.py", line 29, in <module>
import ssl
File "C:\Users\metni\anaconda3\lib\ssl.py", line 98, in <module>
import _ssl # if we can't import it, let the error propagate
ImportError: DLL load failed: The specified module could not be found.
It seems like this is referring to a package called jsonschema which is not installed apparently, but running a pip install jsonschema does not solve the problem for me...
In the answer above I also have two two commands but they are a bit different
source /opt/anaconda/bin/activate root
and then
jupyter notebook
It seems like you have to go into your anaconda directory then bin, then you can
"activate root"
It also looks like you need to install some other dependencies for jupyter to run. Where is says
Jupyter notebook format depends on the jsonschema package:
https://pypi.python.org/pypi/jsonschema
Please install it first.
So if the above doesn't work try installing the other dependency and try again
Based on this answer from a similar question, I managed to solve the problem by running these two commands:
conda update -n root conda
conda update --all
I thus updated all packages, and the notebook is now launching.

Failed to import dataclasses module

Today I installed python 3.7 from apt-get to try out the new dataclasses module. I installed it seperately because python3.6 wasn't upgradeable to 3.7.
When I type: python3.7 --version, it gives me: >>> Python 3.7.0a2 as my current version.
The problem is that I can't seem to import dataclasses.
my import statement is: from dataclasses import dataclass as instructed here
This is the error message it's giving me:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'dataclasses'
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
import apport.fileutils
File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
from apport.packaging_impl import impl as packaging
File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 24, in <module>
import apt
File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
Original exception was:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'dataclasses'
I even tried installing the dataclasses module with pip3: pip3 install dataclasses. Not sure if that's necessary though.
Any suggestion on what might be the problem?
[EDIT] Just tried it with a python3.6 console and it worked fine .. weird.
As suggested by #wim python3.7 -m venv venv_dir
This command will:
Use python3.7 to run the command
The -m flag tells the interpreter to run the next argument as a script
venv is a module, and because of the -m flag it will be run as a script
Finally, the venv_dir is given to the venv module as an argument which
this module will use to create a virtual environment directory at
Once this command is run now you'll have a nice sandbox for messing around/testing the dataclasses module.
To activate this virtual environment be sure to run source venv_dir/bin/activate before you begin. This command will run the script at venv_dir/bin/activate to set up the necessary environment variables and other things for you
To deactivate, simply run deactivate after activating

Permission denied module importation with Spacepy

I need Spacepy package. I installed it carefully following these instructions with no particular problem. At the end of the installation, I did:
python
>>> import spacepy
And it worked just fine. Later on I tried to run a script which uses spacepy and I got an import error with this package. Then I did:
python
>>> import spacepy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "spacepy/__init__.py", line 329, in <module>
_read_config(rcfile)
File "spacepy/__init__.py", line 297, in _read_config
_write_defaults(rcfile, defaults)
File "spacepy/__init__.py", line 200, in _write_defaults
f = open(rcfile, 'r+t') #Avoid race condition, open for read and write
IOError: [Errno 13] Permission denied: '/home/arnaud/.spacepy/spacepy.rc'
>>>
I tried:
cd ~/.spacepy
chmod +rwx spacepy.rc
But it didn't change anything. I also tried to run python with sudo, and it does not work as well, although I have a different error:
sudo python
>>> import spacepy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/spacepy/__init__.py", line 131, in <module>
from . import datamodel
File "/usr/local/lib/python2.7/dist-packages/spacepy/datamodel.py", line 178, in <module>
from . import toolbox
File "/usr/local/lib/python2.7/dist-packages/spacepy/toolbox/__init__.py", line 48, in <module>
from spacepy import time as spt
File "/usr/local/lib/python2.7/dist-packages/spacepy/time.py", line 101, in <module>
import dateutil.parser as dup
ImportError: No module named dateutil.parser
In addition, when running python with sudo, I cannot load all the other packages (numpy, scipy, etc...). I installed python 2.7 with anaconda.
Do you know something I could try ?
I did the exact same procedure on another computer and it worked with no problem.
I figured it out. Change spacepy.rc file permissions was indeed the good solution, but I did not apply it to all users with 'a' flag. Doing:
cd ~/.spacepy
chmod a+rwx spacepy.rc
Solved the problem

Categories