Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use a library locally instead of installing it

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

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

Question:
How can I use this library without installing it?
basically I want to replace: import tweepy with import local_folder/tweepy/*

like image 910
Trevor Hickey Avatar asked Jan 30 '12 05:01

Trevor Hickey


People also ask

How we can import Python modules without installing?

If you are not able to install modules on a machine(due to not having enough permissions), you could use either virtualenv or save the module files in another directory and use the following code to allow Python to search for modules in the given module: >>> import os, sys >>> file_path = 'AdditionalModules/' >>> sys.

How do I install a Python package locally?

To install a package that includes a setup.py file, open a command or terminal window and: cd into the root directory where setup.py is located. Enter: python setup.py install.

How do I install libraries without pip?

To install any python library without pip command, we can download the package from pypi.org in and run it's setup.py file using python. This will install the package on your system.


2 Answers

EDIT: 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 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.


There are a few possibilities:

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.

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

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.

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.

In your code, write:

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

import tweepy
like image 88
HardlyKnowEm Avatar answered Oct 17 '22 10:10

HardlyKnowEm


Simple and clean solution:

import sys
sys.path.insert(0, '/absolute/path/to/package')
import some.cool.package

some.cool.package.doCoolStuff()

other answer with 'append' doesn't work with packages which are installed as well

like image 17
user2846569 Avatar answered Oct 17 '22 12:10

user2846569