简体   繁体   English

添加Python脚本,以便可以在另一个Python脚本中导入

[英]Add Python script so can be imported in another Python script

So I know in windows you can just add the file to the Lib folder and then just add import filename to the python script. 所以我知道在Windows中您可以将文件添加到Lib文件夹,然后将导入文件名添加到python脚本。

Is it possible to do this on Ubuntu in anyway as I need to import this file to make a project work. 无论如何,是否有可能在Ubuntu上执行此操作,因为我需要导入此文件以使项目正常工作。 Link to file need to access 链接到文件需要访问

I toured the github repo for a bit, it should be possible to simply copy CMUTweetTagger.py to your folder where yourapp.py is located (same level) then 我参观了一下GitHub的回购,应该可以简单地复制CMUTweetTagger.py到您的文件夹, yourapp.py位于(同级别),然后

import CMUTweetTagger

CMUTweetTagger.runtagger_parse(...)

Alternatively, since ark-tweet-nlp-python is a package (has got __init__.py in it) 另外,由于ark-tweet-nlp-python是一个程序包(其中包含__init__.py

You can copy the whole ark-tweet-nlp-python folder into eg ark_tweet_nlp_python folder (again same level as your script), eg by cloning it 您可以将整个ark-tweet-nlp-python文件夹复制到ark_tweet_nlp_python文件夹(与脚本相同级别),例如通过克隆

Git clone: Git克隆:

git clone https://github.com/ianozsvald/ark-tweet-nlp-python ark_tweet_nlp_python

Use it as a module: 用作模块:

from ark_tweet_nlp_python import CMUTweetTagger

You need to: 你需要:

import sys
print sys.path
sys.path.append('/path/to/lib/dir')
import mylib

Also, if the python script you are trying to import is in a directory, you need to make sure that you have an init .py file in that folder. 另外,如果您要导入的python脚本位于目录中,则需要确保该文件夹中有一个init .py文件。 It can be empty ( touch /path/to/lib/dir/__init__.py ) before an import will work. 在导入之前,它可以为空(请touch /path/to/lib/dir/__init__.py )。 You can ls /path/to/lib/dir to see if there is an init .py file. 您可以通过ls /path/to/lib/dir命令查看是否有一个初始化 .py文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM