PyFoam problem - ImportError: No module named PyFoam - python

I just install PyFoam using:
pip install PyFoam
Then I wrote a test.py file using:
import PyFoam
Now I got an error message:
import PyFoam
ImportError: No module named PyFoam
Then I tried install again, and I got this:
pip install PyFoam
Requirement already satisfied: PyFoam in ./.local/lib/python3.8/site-packages (2021.6)
Requirement already satisfied: numpy in ./.local/lib/python3.8/site-packages (from PyFoam) (1.23.0)
Could anyone help? Tks.

The issue is likely that you use one version of pip and another version of the Python interpreter. This is particularly common if you have several Python installations on your system. Check that these versions match with each other.
For example, run
python --version
pip --verion
You should see output similar to the following:
Python 3.10.2
pip 22.1.2 from /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pip (python 3.10)
Notice the matching version numbers. I am verifying this on my system with Homebrew, under MacOS, but I assume that it's similar on other systems. Here, different instances of "python" and "pip" binaries are suffixed with the version number. So, if your version of "pip" says "3.8", try specifying "python3.8" as interpreter.
It might also be helpful for troubleshooting to know of the "-m" switch, in order to locate a module. In your example, you would run:
python -mPyFoam
Notice the capitalization for this module, which can differ between the one being used to install the module through pip and when importing it. On my system, as an example, it outputs:
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3: No module named PyFoam.__main__; 'PyFoam' is a package and cannot be directly executed
The error message is fine. It does still mean the module was found. That is in contrast to an output like the following:
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3: No module named pyfoam
The -m switch is documented under https://peps.python.org/pep-0338/.
Another possibility might be that you need to check and configure your Python module search path, as described here https://docs.python.org/3/tutorial/modules.html.
When a module named spam is imported, the interpreter first searches
for a built-in module with that name. These module names are listed in
sys.builtin_module_names. If not found, it then searches for a file
named spam.py in a list of directories given by the variable sys.path.
sys.path is initialized from these locations:
The directory containing the input script (or the current directory
when no file is specified).
PYTHONPATH (a list of directory names, with the same syntax as the
shell variable PATH).
The installation-dependent default (by convention including a
site-packages directory, handled by the site module).
Note On file systems which support symlinks, the directory containing
the input script is calculated after the symlink is followed. In other
words the directory containing the symlink is not added to the module
search path. After initialization, Python programs can modify
sys.path. The directory containing the script being run is placed at
the beginning of the search path, ahead of the standard library path.
This means that scripts in that directory will be loaded instead of
modules of the same name in the library directory. This is an error
unless the replacement is intended. See section Standard Modules for
more information.
It's possible to try to think of other causes. But without any additional information about your setup it would probably be too speculative and become confusing for other people reading this answer, so I will leave it at these two causes, which I believe to be the most common for this type of issue.

Related

python: No module named

I am compiling a bunch of package in a gentoo OS and while compiling I get this error.
execute_process(/tmp/gentoo/usr/bin/python -m rosidl_adapter --package-name
rosidl_generator_c --arguments-file
/tmp/gentoo/var/tmp/portage/ros-galactic/rosidl_generator_c-2.2.1-r2/work/rosidl_generator_c-2.2.1_build/rosidl_adapter__arguments__rosidl_generator_c_interfaces.json
--output-dir
/tmp/gentoo/var/tmp/portage/ros-galactic/rosidl_generator_c-2.2.1-r2/work/rosidl_generator_c-2.2.1_build/rosidl_adapter/rosidl_generator_c
--output-file
/tmp/gentoo/var/tmp/portage/ros-galactic/rosidl_generator_c-2.2.1-r2/work/rosidl_generator_c-2.2.1_build/rosidl_adapter/rosidl_generator_c_interfaces.idls)
returned error code 1:
/tmp/gentoo/usr/lib/python-exec/python3.8/python: No module named
rosidl_adapter
From what I understand is that python simply can't find the module. Then I checked all the packages installed using
python3 -m pip list
And the package doesn't appear in this list. Is it because I didn't install it through pip ?
Also I was able to find rosidl_adapter package under another folder and added this path to the python path. Rerun the previous command to list every package but this one would print the exact same thing.
How and what do I have to modify in order for python to actually see my package.

Module dependencies

I am using ray library of python but I got an error even if I have the mentioned file.
The error is the following:
FileNotFoundError: Could not find module 'C:\Users\boezi\PycharmProjects\AWSDeepRacerChallenge\env\lib\site-packages\ray_raylet.pyd' (or one of its dependencies). Try using the full path with constructor syntax.
pip did not recognize the library, therefore I downloaded the wheel archive, I extracted it and I put the content in the site-packages folder of my virtual environment.
The package is recognized and I can use automatic filliing with my IDE but I have the aforementioned problem.

vtk build succeeds but python says module doesn't exist?

I'm building vtk on Cent OS. I would like to install the python wrappers, and I've followed the build directions to do so.
I've installed under prefix ~/local. Things appear to have gone successfully:
vtk, vtkpython, and other vtk executables appear in my ~/local/bin/ directory. vtk libraries appear in ~/local/lib.
However, when I run python -c "import vtk", I see:
ImportError: No module named vtk
So apparently, something is not correctly pointing to something else, although I'm not sure what I'm missing since I've followed the build directions.
Here is the value of a particular shell variable that seems to show up on a related question:
echo $LD_LIBRARY_PATH
=> ~/local/lib
Check where is the python module. In my system (where VTK was not built following those instructions, but should not be too different) there is a folder in lib/python2.6/site-packages/vtk/ , which contains __init__.py and all the vtk classes (e.g vtkCommonCore.py , vktCommonCorePython.so ..).
If you find that folder, add its parent folder to the PYTHONPATH environment variable (or in your python distribution site-packages folder, create a file vtk.pth which contains that folder).
So, in my system, I have:
export PYTHONPATH=/mypath/INSTALL/lib/python2.6/site-packages/:$PYTHONPATH
See also http://www.vtk.org/Wiki/VTK/Tutorials/PythonEnvironmentSetup

How to import a module from PyPI when I have another module with the same name

I'm trying to use the lockfile module from PyPI. I do my development within Spyder. After installing the module from PyPI, I can't import it by doing import lockfile. I end up importing anaconda/lib/python2.7/site-packages/spyderlib/utils/external/lockfile.py instead. Spyder seems to want to have the spyderlib/utils/external directory at the beginning of sys.path, or at least none of the polite ways I can find to add my other paths get me in front of spyderlib/utils/external.
I'm using python2.7 but with from __future__ import absolute_import.
Here's what I've already tried:
Writing code that modifies sys.path before running import lockfile. This works, but it can't be the correct way of doing things.
Circumventing the normal mechanics of importing in Python using the imp module (I haven't gotten this to work yet, but I'm guessing it could be made to work)
Installing the package with something like pip install --install-option="--prefix=modules_with_name_collisions" package_name. I haven't gotten this to work yet either, but I'm guess it could be made to work. It looks like this option is intended to create an entirely separate lib tree, which is more than I need. Source
Using pip install --target=lockfile_from_pip. The files show up in the directory where I tell them to go, but import doesn't find them. And in fact pip uninstall can't find them either. I get Cannot uninstall requirement lockfile-from-pip, not installed and I guess I will just delete the directories and hope that's clean. Source
So what's the preferred way for me to get access to the PyPI lockfile module?

Python 2.4 Doesn't Load Home-Directory Packages

I'm trying to get a Python package to install to my home directory because I don't have the privileges to install it system-wide.
The package is PyProj, and I am trying to install it using python setup.py install --home=~ (with Python 2.4.3), as recommended in the Python documentation. The package compiles successfully and copies itself to what I assume are the correct directories (the directory ~/lib64/python/pyproj appears during install).
But, when I load Python up and type import pyproj, I'm told ImportError: No module named pyproj.
Any thoughts on what might be going on?
You'll need to set PYTHONPATH to tell Python where to locate your locally installed packages.
For example:
[you#home]$ export PYTHONPATH="~/lib64/python"
Or, to do this within the interpreter (or script):
import sys, os
sys.path.append(os.path.expanduser("~/lib64/python"))
For more information on how Python locates installed modules, see section on The Module search Path in docs.
~/lib64/python/pyproj is not part of your PYTHONPATH. There are two or three ways around this, depending on your needs.
The first is to directly modify the path in your module, suitable if you're only going to use it from one module. As noted in the comments, this method does not do expansion on the '~' character.
import sys
sys.path.append('/home/username/lib64/python')
import pyproj
The second way is to add ~/lib64/python/pyproj to your system's PYTHONPATH, through whatever method your system suggests. A line in .bash_profile is shown below.
export PYTHONPATH=$PYTHONPATH:~/lib64/python/pyproj
See the Python Documentation for more details.

Categories