简体   繁体   English

Freeling Python API处理示例,在其他代码上获取导入错误

[英]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.

相关问题 Python - Plotly - 示例代码错误(iplot) - Python - Plotly - sample code error (iplot) 即使遵循示例代码,导入 spaCy 神经核模块的另一个错误 - Another error to import spaCy neuralcoref module even following the sample code 如何从其他文件导入 Python 代码 - How to Import Python Code from Other Files 其他Python文件的python导入错误 - python import error for other python files 为什么 python 导入在 VS 代码中不起作用? - Why is python import not working in VS code? 为什么我的 python 代码返回导入错误? - Why is my python code returning an import error? 如何修复Python中的“无法从'api'导入名称'get_seomoz_data'”错误 - How to fix "cannot import name 'get_seomoz_data' from 'api' " error in Python 从单元测试调用我的代码时,为什么我会收到多处理的导入错误? (PyCharm Python 3) - Why do I get an import error for multiprocessing when my code is called from a unittest? (PyCharm Python 3) GET api 请求在 POSTMAN 中有效,但在 python 代码中无效,我想将响应下载为 Z628CB5675FF524F3FEE7AAE91 - GET api request is working in POSTMAN but not in python code , I want to download the response as csv Python plotly 在命令行中工作,但不在 python 文件中 - 导入错误 - Python plotly is working in command line but not in python file - import error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM