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

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

问题描述

脚本:

我在python中编写了一个脚本,偶尔会向Twitter发送推文

它只使用一个名为: tweepy

安装好它的库之后,很棒。

Script:
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, great.

问题:

我想在我无权安装任何东西的服务器上托管脚本

如果我可以从我所拥有的文件夹中本地包含它,那将是很棒的。

截至目前,我需要包含在我的顶部文件是:

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文件夹(DOES有 __ init __。py 我认为重要的文件。

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

问题:

如何在不安装的情况下使用此库?

基本上我想替换: import tweepy with import local_folder / tweepy / *

这可能只是python常识,但我卡住了!

this might just be python common sense, but I'm stuck!

推荐答案

编辑:此答案已过时。您应该使用 VirtualEnv 。如果您因某种原因对第三方软件过敏(在这种情况下,为什么要安装库?),有一种叫做 venv ,字面意思是内置于python3中,所以没有理由不使用某种虚拟化。 (然而,大多数活跃在社区中的人更喜欢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 <的脚本/ code>作为当前工作目录。

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

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

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