I'm trying to simulate with python and Mathematica, Python need to generate and save some pictures to the directory of current mma notebook, I know the function that NotebookDirectory[]is to get the path to the folder, so I make variable mynotedirectas a String below:

mynotedirect = ExportString[NotebookDirectory[], "PythonExpression"]

then I add a external language input for Python and input code below

from wolframclient.serializers import export
from wolframclient.evaluation import WolframLanguageSession
from wolframclient.language import wl
session = WolframLanguageSession()
mydirect = wl.mynotedirect
imagefile = mydirect + "appendix_c.png"

but when I type Shift+Enter to run the code, it reports below

unsupported operand type(s) for +: 'WLSymbolFactory' and 'str'

if I make mydirect = wl.mynotedirect.to_string(), it also doesn't work, report

unsupported operand type(s) for +: 'WLFunction' and 'str'

So, How to convert 'WLSymbolFactory to Python String? Any reply is much appreciated.

  • $\begingroup$ “I add a external language input for Python and input code below” Do you mean you've created an external language cell by typing > and input those lines? If so, then that won't work of course, data cannot be exchanged between Mathematica and Python in this manner. And we don't need that complicated example to illustrate the issue, just try passing something like aaa=1. There already exist a few examples showing the correct way to exchange data in this site e.g. mathematica.stackexchange.com/a/249630/1871 mathematica.stackexchange.com/q/284122/1871 , have a look. $\endgroup$
    – xzczd
    Commented Apr 9 at 6:05
  • $\begingroup$ See also scope section of document of ExternalEvaluate: reference.wolfram.com/language/ref/… $\endgroup$
    – xzczd
    Commented Apr 9 at 6:12
  • $\begingroup$ @xzczd, Yes, typing ">" and input python code directly, I need python module to simulate and process data, finally generate and save some figures. The total python code volume has reached fifty lines, so ExternalEvaluate is not a suitable method for a large amount of python code $\endgroup$
    – Yao Li
    Commented Apr 9 at 8:26
  • $\begingroup$ ……Why do you think so? You can have line break in ExternalEvaluate, too. $\endgroup$
    – xzczd
    Commented Apr 9 at 9:28
  • $\begingroup$ @xzczd, Maybe because I am a beginner using mma, I think the code that a external language input for Python is concentrated in blocks, it is easy to read, annotate, and execute uniformly, The code in ExternalEvaluate is quite scattered $\endgroup$
    – Yao Li
    Commented Apr 9 at 11:38

1 Answer 1


I believe you are misunderstanding the purpose of the Wolfram Client Library for Python and its typical use case. When you do session = WolframLanguageSession(), it opens a new session, completely unrelated to the current session inside your notebook, and that is why your mynotedirect is not defined and returned as a WLSymbolFactory.

You don't actually need to use this library at all. Simply wrap the Wolfram Language expressions wth <* *>:

enter image description here

  • $\begingroup$ Thank you very much for your help. You're right. As a beginner using mma, I did misunderstand the usage of the WCL & WL modules. Thank you again for your answer $\endgroup$
    – Yao Li
    Commented Apr 9 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.