Create new anki profile from python

Hi all,
I want to create new anki profiles on need bases.
how can I create a new anki user profile from python code?

which command to use?

could you please help?

Try:

mw.pm.create("My Profile")
1 Like

thank you. I am getting an error “mw” is not defined

I added the following import also still

from aqt import mw

now the error is “cannot import name ‘mw’ from ‘aqt’”

What’s your environment? And how are you installing the aqt package? The code only works in an add-on.

My envionment is my laptop, developing a web application and I installed aqt from command line with "pip install " command.

My code is like below

from aqt import mw
mw.pm.create(“sdfff”)

the error I am getting is No module named ‘PyQt6.QtWebEngineCore’

You probably have to create a ProfileManager object and avoid using mw then, since that doesn’t work outside add-ons. Check the source here: anki/qt/aqt/profiles.py at 312d396505a4806b76b74dfda6d276ec595e17ed · ankitects/anki · GitHub

Thank you.
when I try that I am getting below error

"ModuleNotFoundError: No module named ‘PyQt6.QtWebEngineCore’

I have PyQt6 already installed actually.

from aqt import *
ProfileManager.create(“fsdf”)

When I type the below line
“from PyQt6” and when I press Dot, I do not see QtWebEngineWidgets in the available list

Does installing aqt using pip install aqt[qt6] help?

Hi Adbo,
I tried that, but that did not help. Still the same error

thank you

Sorry, I meant pip install aqt[qt6].

great, that worked but now I have another issue
it expects 2 parameters

My code is below lines

import sys
from aqt import *
ProfileManager.create(“mY profile”)

now the error is “ProfileManager.create() missing 1 required positional argument: ‘name’”

from aqt.profiles import ProfileManager

pm = ProfileManager(ProfileManager.get_created_base_folder(None))
pm.setupMeta()
pm.create("my profile")

Thanks abdo,
I ran the command, but after running those commands, anki is not opening.

image

I had to completely clean uninstall and reinstall Anki to get it back working.

So running the command, is broke anki with error

AttributeError: ‘NoneType’ object has no attribute ‘execute’

But after complete uninstall/install, it seemed working, I will double check and let you know, thank you.

Regards
Ravindar

Hi abdo, once the profile got created, the corresponding folder is not being created.
How can I make the folder be created when the profile is created?

pm.profileFolder() command is not helping either because it only creates a empty folder but not the content inside.

I need the operation exactly when we do “switch profile” in anki, that will create the folder and it’s contents inside

Use this to create the profile folder after running the previous code snippet:

from anki.collection import Collection
col = Collection('profile_name/collection.anki2')

Thanks abdo,
unfortunately, That command throws error

anki.errors.DBError: DbError { info: “SqliteFailure(Error { code: CannotOpen, extended_code: 14 }, Some("unable to open database file: …myprofile7\\\\collection.anki2"))”, kind: Other }

I missed the step of creating the folder. This should work:

import os

profile_path = os.path.join(pm.base, profile_name)
os.mkdir(profile_path)
col = Collection(os.path.join(profile_path, "collection.anki2"))

Lovely, that worked pretty good, Thanks abdo

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.