ModuleNotFoundError: No module named 'yaml'

Dear community members,
I have some trouble running tox-lsr integration tests. The following code block shows an example of the command I run and the errors I get:

$ tox -e qemu-ansible-core-2.11 -- --config ~/.config/linux-system-roles.json --image-name fedora-35 -u root -- tests/tests_default.yml
qemu-ansible-core-2.11 installed: ansible-core==2.11.10,beautifulsoup4==4.10.0,cffi==1.15.0,cryptography==36.0.2,Jinja2==3.1.1,MarkupSafe==2.1.1,packaging==21.3,productmd==1.33,pycparser==2.21,pyparsing==3.0.7,PyYAML==6.0,resolvelib==0.5.4,ruamel.yaml==0.17.21,ruamel.yaml.clib==0.2.6,six==1.16.0,soupsieve==2.3.1
qemu-ansible-core-2.11 run-test-pre: PYTHONHASHSEED='885645574'
qemu-ansible-core-2.11 run-test: commands[0] | python /home/tronde/python-venv/tox/lib64/python3.10/site-packages/tox_lsr/test_scripts/runqemu.py --config /home/tronde/.config/linux-system-roles.json --image-name fedora-35 -u root -- tests/tests_default.yml

ansible-playbook [core 2.11.10] 
  config file = None
  configured module search path = ['/home/tronde/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/tronde/ansible/roles/pam_pwd/.tox/qemu-ansible-core-2.11/lib/python3.10/site-packages/ansible
  ansible collection location = /home/tronde/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/tronde/ansible/roles/pam_pwd/.tox/qemu-ansible-core-2.11/bin/ansible-playbook
  python version = 3.10.3 (main, Mar 18 2022, 00:00:00) [GCC 11.2.1 20220127 (Red Hat 11.2.1-9)]
  jinja version = 3.1.1
  libyaml = True
No config file found; using defaults
[DEPRECATION WARNING]: ANSIBLE_CALLBACK_WHITELIST option, normalizing names to new standard, use ANSIBLE_CALLBACKS_ENABLED instead. This feature will be removed from ansible-core in version 2.15. 
Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
[WARNING]:  * Failed to parse /home/tronde/ansible/roles/pam_pwd/.tox/standard-inventory-qcow2 with script plugin: Inventory script (/home/tronde/ansible/roles/pam_pwd/.tox/standard-inventory-
qcow2) had an execution error: Traceback (most recent call last):   File "/home/tronde/ansible/roles/pam_pwd/.tox/standard-inventory-qcow2", line 14, in <module>     import yaml
ModuleNotFoundError: No module named 'yaml'
[WARNING]:  * Failed to parse /home/tronde/ansible/roles/pam_pwd/.tox/standard-inventory-qcow2 with yaml plugin: We were unable to read either as JSON nor YAML, these are the errors we got from
each: JSON: Expecting value: line 1 column 1 (char 0)  Syntax Error while loading YAML.   mapping values are not allowed in this context  The error appears to be in
'/home/tronde/ansible/roles/pam_pwd/.tox/standard-inventory-qcow2': line 30, column 4, but may be elsewhere in the file depending on the exact syntax problem.  The offending line appears to be:
try:    ^ here
[WARNING]:  * Failed to parse /home/tronde/ansible/roles/pam_pwd/.tox/standard-inventory-qcow2 with ini plugin: /home/tronde/ansible/roles/pam_pwd/.tox/standard-inventory-qcow2:9: Expected
key=value host variable assignment, got: os

I guess the main issue is the following error:

[WARNING]:  * Failed to parse /home/tronde/ansible/roles/pam_pwd/.tox/standard-inventory-qcow2 with script plugin: Inventory script (/home/tronde/ansible/roles/pam_pwd/.tox/standard-inventory-
qcow2) had an execution error: Traceback (most recent call last):   File "/home/tronde/ansible/roles/pam_pwd/.tox/standard-inventory-qcow2", line 14, in <module>     import yaml
ModuleNotFoundError: No module named 'yaml'

But as far as I could tell the appropriate module has been installed:

Requirement already satisfied: pyyaml in /home/tronde/python-venv/tox/lib/python3.10/site-packages (6.0)

I’m stuck here and don’t know how to troubleshoot and solve this issue. Any help is appreciated. Please bear with me, as I don’t have much experience with python and venv.

Best regards,
Tronde

You probably need to install the python3-pyyaml package?

Should this stuff be run in a virtualenv maybe so that you can install whatever python packages you need?

2 Likes

Tox installed that package into the virtualenv it created, so as long as the ansible-core stuff being run is using that virtualenv, the package should be there.

If I were the OP, I’d ask about this on the Python Discourse, since this is really a Python/Tox issue, not related to Fedora as best I can tell.

The package python3-pyyaml is not available:

$ python3 -m pip install python3-yaml
ERROR: Could not find a version that satisfies the requirement python3-yaml (from versions: none)
ERROR: No matching distribution found for python3-yaml

I’m going to. I was asking here first, because I already have an account here and was hoping that someone knows this problem and would be able to help. :slight_smile:

you’re mixing dnf and pip package names :slight_smile:

For pip, the package is PyYAML:

For dnf, it’s python3-pyyaml:

sudo dnf install python3-pyyaml

I hope you’re running that command in a virtualenv too—it’s not a good idea to mix system (dnf) packages and user-installed packages. It can cause issues like breaking dnf itself :frowning:

2 Likes

Oh sorry. I tried the following, too:

$ python3 -m pip install python3-pyyaml
ERROR: Could not find a version that satisfies the requirement python3-pyyaml (from versions: none)
ERROR: No matching distribution found for python3-pyyaml

Maybe I’m still using the wrong package name.

isn’t the package name PyYAML?
See PyYAML · PyPI

Shouldn’t that be:

python3 -m pip install pyyaml  # or PyYAML (both work)

the python3-pyyaml is for the Fedora package, because we preface all our python packages with the python3- prefix to make them easier to find:

https://docs.fedoraproject.org/en-US/packaging-guidelines/Python/#_naming

2 Likes

Thank you all for engaging in this discussion. I would like to share some new findings.

(tox) [tronde@t14s ~]$ which python3
~/python-venv/tox/bin/python3
(tox) [tronde@t14s ~]$ python3
Python 3.10.4 (main, Mar 25 2022, 00:00:00) [GCC 11.2.1 20220127 (Red Hat 11.2.1-9)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import yaml
>>> yaml
<module 'yaml' from '/home/tronde/python-venv/tox/lib64/python3.10/site-packages/yaml/__init__.py'>
>>>

So it seems when I start the interactive python3 interpreter from within my virtualenv and import the YAML module that everything works as expected. Do you agree?

I hope this information will help to find the cause for the problem.

If you installed the package there in your virtualenv, yes, that would make sense.

(Not all virtual environments include all packages—one installs whatever python modules they need in them)

Yes, I’ve installed the package there in this virtualenv. It’s the same virtualenv where I run the tox command from my initial post.

I don’t understand why the tox job won’t find the YAML module.

Does the tox configuration for your environment mention that pyyaml is required?

https://tox.wiki/en/latest/

1 Like

Which tox are you using? You should be using python3-tox installed via dnf. There was a bug in earlier versions of this package, that did not allow user installed plugins, but that has been fixed. Also, you should not run tox if you are “inside” of a virtualenv. tox will create a venv to run its commands - that is a primary purpose of tox - to create python venvs to run commands in.

Also, I suggest to use qemu-ansible-core-2.12 as 2.12 is the latest supported version.

Once you do this, if it is still failing - try this

. .tox/qemu-ansible-core-2.12/bin/activate  # start a python venv in this env
.tox/standard-inventory-qcow2
deactivate
1 Like

There’s nothing wrong with running tox in a virtualenv. If you want to use a newer version than your distribution provides, or you want to use a Python interpreter newer than your distribution provides, or many other reasons, it’s just fine to install tox using pip in a virtualenv.

1 Like

it’s just fine to install tox using pip in a virtualenv.

Sure, but in this case, the other linux-system-roles developers use the Fedora python3-tox package version of tox, so it’s easier for us to help if we’re all using the same version of tox.

2 Likes