ImportError: cannot import name 'YamlModel' from 'pydantic_yaml' - python

I am working at blender script, where I want to use YAML. So I try to import it:
from pydantic import BaseModel
from pydantic_yaml import YamlModel
But when I run my script, this error will occure:
ImportError: cannot import name 'YamlModel' from 'pydantic_yaml' (C:\Program Files\Blender Foundation\Blender 3.0\3.0\python\lib\site-packages\pydantic_yaml\__init__.py)
I have installed pydantic and pydantic_yaml on both Pythons (Blender one and the common one), if they wouldn't be installed, there would be different error. I tried googling, but google had very few results for this problem. Also, when I open that __init__.py file, there is clearly YamlModel. Thank you for any kind of help.

I found the same error and it is related how pydantic-yaml got installed.
There is a bug in the package. you need to install optional dependencies as well (so called extras).
pip install pedantic-yaml[pyyaml,ruamel]
or manually install pyyaml ruamel please make sure that that Taumel version is <=0.18

Related

ModuleNotFoundError but the module name exists in one of the directories in sys.path

The issue
I've pip installed a library called disagree which installed and upgraded without any issues, confirming that the latest version had been successfully installed.
When running import disagree I get the error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'disagree'
Identifying what's causing the issue
Section 6.1.2 in https://docs.python.org/3/tutorial/modules.html#packages says that when a module is imported, if it is not in the sys.builtin_module_names, then it proceeds to search for it in the file paths specified in sys.path.
When I run sys.path I get:
['', '/Users/oliverprice/miniconda3/lib/python38.zip',
'/Users/oliverprice/miniconda3/lib/python3.8',
'/Users/oliverprice/miniconda3/lib/python3.8/lib-dynload',
'/Users/oliverprice/.local/lib/python3.8/site-packages',
'/Users/oliverprice/miniconda3/lib/python3.8/site-packages']
Indeed, looking into '/Users/oliverprice/miniconda3/lib/python3.8/site-packages' I can see the module that I've installed. However, it only has the .dist-info file for the package, not the actual package folder. I.e. rather than
name
name-version.dist-info
the only thing present is:
disagree-1.2.6.dist-info
So it looks like there is no actual package in there, and just the .dist-info. Specifically, this is a snapshot of what is in there:
defusedxml
defusedxml-0.6.0.dist-info
dill
dill-0.3.4.dist-info
disagree-1.2.6.dist-info
distutils-precedence.pth
docutils
docutils-0.16.dist-info
easy_install.py
entrypoints-0.3.dist-info
Questions
Is this the reason it is failing to import? If not, what is the reason?
If so, why has this happened?
I have the same issue here. This solution below worked for me.
First of all, check the version of python that you have (must be between python3.8 and 3.10 (included). No python3.7 and python3.11 as I understand.
To check it do this in your notebook/python:
import sys
print(sys.version)
If it matches with the 3.8-3.10 range, then you're ok and skip this step, otherwise you may need to do (in the terminal):
virtualenv venv --python=python3.8
source venv/bin/activate
Let's now install this library from source so that we have the needed source files. Run this in a terminal:
git clone https://github.com/o-P-o/disagree.git
cd disagree
# requirements fixed
pip install numpy pandas mathx scipy tqdm
# install this
pip install .
Now you should be able to import disagree, but with a twist:
from disagree import disagree
# or for example
from disagree.disagree.agreements import BiDisagreements
Please let me know if this works for you.
I will be doing a pull request to fix this library.
Martino

ImportError for some but not all classes in package installed from TestPyPI

I am trying to publish a package on PyPI. First, I uploaded it to TestPyPI then installed it on a fresh Conda environment with
python3 -m pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ neuralpp
However I am unable to import some classes from the package. The minimal example below illustrates this:
# main.py
from neuralpp.inference.graphical_model.representation.table.pytorch_log_table import PyTorchLogTable
from neuralpp.inference.graphical_model.representation.factor import PyTorchTableFactor
When I run python main.py, the first import (PyTorchLogTable) works, but not the second (PyTorchTableFactor). See the error message at the end of the question.
I checked the package files (within the Conda environment) and the required files are present. This also works fine in the original project I uploaded.
Because the error message mentions __init__.py, I've added __all__ = ['PyTorchTableFactor'] to it, even though I know the use of __all__ is not mandatory. However, this has not changed anything. Besides, the __init__.py for PyTorchLogTable is also empty and that is imported correctly, further confirming that this is not the issue.
What is wrong here, and how can I fix it?
Error message:
File "main.py", line 2, in <module>
from neuralpp.inference.graphical_model.representation.factor import PyTorchTableFactor
ImportError: cannot import name 'PyTorchTableFactor' from 'neuralpp.inference.graphical_model.representation.factor' (/Users/rodrigobraz/opt/anaconda3/envs/test/lib/python3.8/site-packages/neuralpp/inference/graphical_model/representation/factor/__init__.py)

How to know the package or module name from import statement in Python?

I have a python file and the developer of that code has left the organization. When I run the code I get the following error.
import dataAnalysis as DV ModuleNotFoundError: No module named
'dataAnalysis'
I provide below the brief snippet of the python file "main.py" below.
import dataAnalysis as DV
def performCheck():
... other code
... other code
i = DV.addGraph( pathplus)
Here my question is , how to know the actual module or package name of "dataAnalysis" from the above import statement so that I can make "pip install ". However, I tried to install DataAnalysis module, still it does not work.
Is there any way to get the module or package name to install from the import statement in python ?
Go to console or terminal and run command pip install dataAnalysis. If permission denied, then make sure you have enough privilege to install a package.
Update:
In my opinion pip package DataAnalysis is a library that can be used for pre-processing a csv file. As per your given code, it looks like adding a graph so may be it could be a local package. Check dataAnalysis folder in your project with __init__.py file inside.

Python module from GitHub installed using setup.py can't see own submodules

I downloaded python-somelib-master.zip from GitHub hoping to use the API it provides. I ran
python setup.py install
And it apparently completed successfully:
Writing D:\SOFT\Python3\Lib\site-packages\python-somelib-1.0-py3.5.egg-info
which then introduced D:\SOFT\Python3\Lib\site-packages\somelib.
However, when I try to import something from there in my script:
from somelib import SubModule
I get
File "D:\SOFT\Python3\lib\site-packages\somelib\__init__.py", line 1, in <module>
from base import SubModule
ImportError: No module named 'base'
I confirmed that base.py is present in D:\SOFT\Python3\Lib\site-packages\somelib.
Did I not properly install the module?
You must install modules dependencies in your machine too. If you are using Ubuntu, you can easily do it with "apt-get". Hope it helps! xD

Python run package without installing

As part of my build system, I am using a modified version of a Python package (cogapp). I don't want to install the package because:
I've modified the package and don't want to worry about collision with unmodified versions which may already be installed.
It's nicer if the users of the build system don't need to install extra packages.
However, I'm having problems with using the package if it's not installed. If it is installed, I can run:
python -m cogapp <additional args>
and everything runs as intended.
The package has a __main__.py script:
import sys
from cogapp import Cog
sys.exit(Cog().main(sys.argv))
I tried running this directly, e.g.:
python -m <path>/__main__ <additional_args>
But I get the error:
...
/__main__.py", line 3, in <module>
from cogapp import Cog
ImportError: No module named cogapp
This is probably related to the error I get if I run __init__.py:
from .cogapp import *
The error is:
from .cogapp import *
ValueError: Attempted relative import in non-package
How can I run the package as a package?
EDIT:
I found a fix by removing all the relative imports from cogapp, and removing the -m, i.e. not running as a module. In this instance it's not too bad because it's a small package with only a single directory. However I'm interested in how this should be done in future. There's lots of stuff written around this subject, but no clear answers!
Here's the solution I've come to.
Disclaimer: you are actually installing the package but to a different path than the standard one.
$ mkdir newhome
$ python setup.py install --home=./newhome
$ PYTHONPATH=$PWD/newhome/lib/python <COMMAND_NEEDING_THAT_PACKAGE>

Categories

Resources