1

So this code was working before now I'm getting this error - please help :(

mallet_path = 'C:/mallet/mallet-2.0.8/bin/mallet.bat'

ldamallet_test = gensim.models.wrappers.LdaMallet(mallet_path, corpus=bow_corpus_test, num_topics=20, id2word=dictionary_test)

Error message

This mallet.bat file I'm referencing no longer opens

Sara
  • 1,162
  • 1
  • 8
  • 21

1 Answers1

1

This is because your home directory for Mallet is not set up properly. Even though you have the path to the binary set as a variable, you still must define the environment variable that contains the source of where Mallet is located:

import os
from gensim.models.wrappers import LdaMallet

os.environ['MALLET_HOME'] = 'C:\\mallet\\mallet-2.0.8'

mallet_path = 'C:\\mallet\\mallet-2.0.8\\bin\\mallet'
ldamallet_test = gensim.models.wrappers.LdaMallet(mallet_path, corpus=bow_corpus_test, num_topics=20, id2word=dictionary_test)

Take note that you don't need to add the .bat extension as Windows should execute this natively as it knows it's a batch file. As a final note, you should use double backslashes (\\) for the path separator in Windows. Not doing this depending on what version of Windows you're using may give unexpected behaviour. If you want to avoid headaches, try using os.path.join which will provide the right path separator for you regardless of the OS:

mallet_path = os.path.join('C:', 'mallet', 'mallet-2.0.8', 'bin', 'mallet')
rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • The ".bat" file extension can be assumed when executing via the CMD shell (e.g. using `subprocess.Popen` with `shell=True`) because it's in the `PATHEXT` environment variable. Otherwise it cannot be assumed. This is a shell behavior, not general to Windows. `CreateProcessW` supports executing batch scripts directly via the `ComSpec` shell, but the ".bat" or ".cmd" extension has to be included. Otherwise it only assumes ".exe" as the file extension, and tries to execute any file it finds as a binary image. – Eryk Sun Apr 02 '19 at 23:41