Ask Your Question
1

Cannot import name "Category"

asked 2025-08-18 22:39:59 +0200

babushkat gravatar image

updated 2025-08-19 03:32:39 +0200

Hello! (Tried to post this, told I didn't have enough karma for links, but it's the virasoro algebra from the sagemath documentation, and then the github link for that same site.)

BACKGROUND: I'm attempting to use [the virasoro algebra sagemath stuff]. I literally clicked source (brought me to [the virasoro algebra github page thing] ), and then copy/pasted the source code into a .py file in VS Code running in/on/around WSL:Ubuntu (so that I can use the sage interpreter).

When I import that source file (call it source.py) into another file (call it main.py) and trying to run main.py, I receive an error (see the traceback below). The error is from ~/sage/structure/... files, though, not source.py.

QUESTION: How do I successfully import name Category? Is this a common issue? Do I need to import something else first?

Further Notes/Attempted Solutions, Troubleshooting: The import still fails in the same spot, even if I add from sage.all import * to the preamble.

I installed sage via/with the wsl --install route. I used miniforge3 if that matters. I am not yet technically adept (I am coding-stupid).

There are no other files named sage that could be shadowing the right one.

Compiled extension files (.so / .pyd) do exist for Sage internals. (I used ai for help troubleshooting and it suggested this. I am not entirely sure what it means...)

There are no circular imports; source.py is the only file I'm importing into main.py.

(edited line here ->): I get the error if I run the file in the WSL terminal, too.

Let me know if any other information is needed, and thank you!

Error message: conda run --live-stream --name sage python /home/babushkat/mrns_project/mrns Traceback (most recent call last):

File "/home/babushkat/mrns_project/mrns", line 1, in <module> from Witt_algebra import *

File "/home/babushkat/mrns_project/Witt_algebra.py", line 19, in <module> from sage.categories.lie_algebras import LieAlgebras

File "/home/babushkat/miniforge3/envs/sage/lib/python3.11/site-packages/sage/categories/lie_algebras.py", line 24, in <module> from sage.categories.category import JoinCategory, Category

File "/home/babushkat/miniforge3/envs/sage/lib/python3.11/site-packages/sage/categories/category.py", line 109, in <module> from sage.misc.c3_controlled import _cmp_key, _cmp_key_named, C3_sorted_merge

File "sage/misc/c3_controlled.pyx", line 369, in init sage.misc.c3_controlled (build/cythonized/sage/misc/c3_controlled.c:15760)

File "/home/babushkat/miniforge3/envs/sage/lib/python3.11/site-packages/sage/structure/__init__.py", line 3, in <module> import sage.structure.element

File "sage/structure/element.pyx", line 1, in init sage.structure.element (build/cythonized/sage/structure/element.c:47835)

File "sage/structure/category_object.pyx", line 62, in init sage.structure.category_object (build/cythonized/sage/structure/category_object.c:13626)

ImportError: cannot import name Category

ERROR conda.cli.main_run:execute(127): conda run python /home/babushkat/mrns_project/mrns failed. (See above for error)

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2025-08-19 11:58:10 +0200

rburing gravatar image

It seems like a strange thing to make a copy of a file you already have in your SageMath installation. You are talking about virasoro.py, right? If I make a copy of it and put a main.py next to it with the code

from sage.all import *
from virasoro import *

then running conda run --live-stream --name sage python main.py works for me on Linux. Does that not work for you? And why not avoid the file copy and import from the file you have in your installation?

from sage.all import *
from sage.algebras.lie_algebras.virasoro import *
edit flag offensive delete link more

Comments

I didn't realize that the virasoro.py was built into sage like that. I'm a novice, like I said.

When I type

from sage.all import *
from virasoro import *

in a new file in my project space, I see this error before I run it ("inline error"?): Import "virasoro" could not be resolvedPylancereportMissingImports. I guess VS Code isn't "seeing" my SageMath installation...?

I don't understand, fully, the relationship between SageMath, Conda, Miniforge, WSL, and VS Code. I know sagemath is (basically) a coding package in python, VS Code is an IDE, and WSL is a way to run Linux without partitioning my hard drive. I know SageMath lives on/in the WSL. In VS Code, I have the WSL extension installed that let 's me, quote, "... use VS Code in WSL just as [I] would from Windows."

And ty!

babushkat gravatar imagebabushkat ( 2025-08-20 02:50:37 +0200 )edit
1

If I run conda activate sage and then sage -python -c "from sage.algebras.lie_algebras import virasoro; import inspect; print(inspect.getsourcefile(virasoro))" in the command prompt-looking ubuntu terminal thing (what do I call this? WSL Terminal?), I get the same traceback error list ending with cannot import name Category.

Wait,

from sage.all import *
from sage.algebras.lie_algebras.virasoro import *

WORKS. It does not, presumably, fail to import category. Why is this? If I run the same from virasoro import * in the WSL terminal, it also says no module named 'virasoro'.

And thank you so very much for your help. I appreciate you tremendously!

babushkat gravatar imagebabushkat ( 2025-08-20 03:05:00 +0200 )edit

from virasoro import * works when there is a file named virasoro.py in the same directory (that's how I named my copy of the file). And yes, from sage.all import * (or just import sage.all?) is a way to trigger some setup needed for SageMath when it's used from Python. I don't know exactly why it works, I treat it as a magic incantation. I'm glad you got it working!

rburing gravatar imagerburing ( 2025-08-20 10:36:40 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2025-08-18 22:24:24 +0200

Seen: 17,289 times

Last updated: Aug 18