[英]Freeling Python API working on sample, get Import error on other code
I'm trying out Freeling's API for python. 我正在尝试Freeling的python API。 The installation and test were ok, they provide a sample.py file that works perfectly (I've played around a little bit with it and it works).
安装和测试都没问题,他们提供了一个完美运行的sample.py文件(我用它玩了一点点就可以了)。
So I was trying to use it on some other python code I have, in a different folder (I'm kind of guessing this is a path issue), but whenever I import freeling (like it shows on the sample.py): 所以我试图在其他一些python代码上使用它,在一个不同的文件夹中(我有点猜测这是一个路径问题),但每当我导入freeling(就像它在sample.py上显示):
import freeling
FREELINGDIR = "/usr/local";
DATA = FREELINGDIR+"/share/freeling/";
LANG="es";
freeling.util_init_locale("default");
I get this error: 我收到此错误:
ModuleNotFoundError: No module named 'freeling'.
The sample.py is located on the ~/Freeling-4.0/APIs/Python/ folder, while my other file is located in ~/project/, I dont know if that can be an issue. sample.py位于〜/ Freeling-4.0 / APIs / Python /文件夹中,而我的另一个文件位于〜/ project /中,我不知道这是否是一个问题。 Thank you!
谢谢!
您需要设置PYTHONPATH,以便python可以找到模块,如果它们不在同一个文件夹中。
A simple solution is to have a copy of freeling.py
in the same directory as your code, since python will look there. 一个简单的解决方案是将
freeling.py
的副本与freeling.py
放在同一目录中,因为python会在那里查找。
A better solution is to either paste it in one of the locations where it usually checks (like the lib
folder in its install directory), or to tell it that the path where your file is should be scanned for a module. 更好的解决方案是将其粘贴到通常检查的位置之一(如安装目录中的
lib
文件夹),或者告诉它应该扫描文件的路径以获取模块。
You can check out this question to see how it can be done on Windows. 您可以查看此问题 ,了解如何在Windows上完成此操作。 You are basically just setting the PYTHONPATH environment variable, and there will only be minor differences in how to do so for other OSes.
您基本上只是设置PYTHONPATH环境变量,并且对于其他操作系统的操作方式只会略有不同。 This page gives instructions that should work on Linux systems.
此页面提供了适用于Linux系统的说明。
I like this answer since it adds the path at runtime in the script itself, doesn't make persistent changes, and is largely independent of the underlying OS (apart from the fact that you need to use the appropriate module path of course). 我喜欢这个答案,因为它在脚本本身添加了运行时的路径,不会进行持久的更改,并且在很大程度上独立于底层操作系统(当然,除了你需要使用适当的模块路径这一事实)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.