在本地使用库而不是安装它 [英] Use a library locally instead of installing it

查看:56
本文介绍了在本地使用库而不是安装它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用python编写了一个脚本,偶尔将推文发送到Twitter
它仅使用一个名为 tweepy 的库.安装库后,它就可以使用.

I've written a script in python that occasionally sends tweets to Twitter
It only uses one library called tweepy. After installing the library it works.

问题:
我想将脚本托管在没有特权安装任何服务器的服务器上
如果能从我拥有的文件夹中本地包含它,那就太好了.
截至目前,我需要在文件顶部添加的所有内容是:

Problem:
I would like to host the script on a server where I do not have privileges to install anything
It would be great if I can just include it locally from the folder I've got it in.
As of right now, all I need to include at the top of my file is:

import tweepy

tweepy文件夹(确实具有 __ init__.py文件,我认为这很重要.

the tweepy folder (DOES have a __init__.py file which I believe is important.

问题:
如何在不安装此库的情况下使用它?
基本上我想替换为: import tweepy import local_folder/tweepy/*

推荐答案

编辑:此答案已过时.您应该使用 VirtualEnv .如果您出于某种原因对第三方软件过敏(在这种情况下,为什么要安装库?),则有一个名为 https://stackoverflow.com/a/41573588/410889 )

This answer is outdated. You should be using VirtualEnv. If you are allergic to third-party software for some reason (in which case, why are you installing libraries?), there is something called venv, that is literally built into python3, so there is no excuse not to use some kind of virtualization. (Most people active in the community prefer VirtualEnv, however. See https://stackoverflow.com/a/41573588/410889.)

VirtualEnv将安装一个本地python解释器,以及一个本地程序包文件夹和所有内容.除了可以完全解决管理特权问题之外,VirtualEnv的最重要功能还在于它可以使您的环境保持独立.如果您有一个需要Foo版本2.3的项目,而另一个需要Foo版本1.5的项目,则不能让它们共享同一环境.您必须使用VirtualEnv将其环境分开.

VirtualEnv installs a local python interpreter, with a local packages folder and everything. In addition to this entirely solving the issue of administrative privileges, the most important feature of VirtualEnv is that it allows you to keep your environments separate. If you have one project that needs Foo version 2.3 and another that needs Foo version 1.5, you can't have them share the same environment; you have to keep their environments separate with VirtualEnv.

有几种可能性:

如果您已经知道如何安装Python模块,则默认的 distutils 设置已经包含了按用户安装的选项.只需运行 python setup.py install --user 而不是 python setup.py install .这是最简单的,因为这不需要添加任何源代码.

If you already know how to install Python modules, the default distutils setup already includes a per-user installation option. Just run python setup.py install --user instead of python setup.py install. This is the easiest, since this does not necessitate the addition of any source code.

您还可以使用 tweepy 目录作为当前工作目录来运行脚本.

You could also run the script with the directory of tweepy as the current working directory.

您可以将一个名为PYTHONPATH的环境变量添加到用于运行脚本的任何环境(例如shell)中,并使其包含 tweepy 的路径.

You could add an environment variable named PYTHONPATH to whatever environment (e.g., the shell) you use to run your script, and make it contain the path to tweepy.

如果所有其他操作均失败,并且您确实要编辑源代码,则需要编辑 sys.path . sys.path 是Python将在其中查找代码的位置的列表.

If all else fails, and you really do want to edit your source code, you'll need to edit sys.path. sys.path is a list of locations where Python will look for code.

在您的代码中,输入:

import sys
sys.path.append("/path/to/your/tweepy/directory")

import tweepy

这篇关于在本地使用库而不是安装它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆