Use a library locally instead of installing it
Asked Answered
C

5

83

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/*

Clotilde answered 30/1, 2012 at 5:21 Comment(1)
I think all you need is for tweepy folder to be visible in the sys.path, and then you use import tweepy as usual. The presence of __init__.py file will make the directory appear to be a module, and code inside that file would be run (once) at import time.Bid
A
82

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://mcmap.net/q/25139/-what-is-the-difference-between-venv-pyvenv-pyenv-virtualenv-virtualenvwrapper-pipenv-etc.)

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
Arthropod answered 30/1, 2012 at 5:32 Comment(6)
What if you want to force using a local library instead of a default library? Based on module search path, python will not look for local modules if the module is already installed on the system. docs.python.org/2/tutorial/modules.htmlWreck
@JeffTsui Well, by default, the first entry in sys.path is the current directory, so by running from the same directory as your module, it'll always load. You can also perform any arbitrary modifications to sys.path; it's just a list. From the wording of the link, I don't think there's any way to use a local library that has the same name as a built-in module, though.Arthropod
@JeffTsui Also, in the couple years of experience I've had since posting this answer, I would say your answer to any path-related issues is that you should be using virtualenv, as one of the other answerers suggested below. VirtualEnv is basically the One True Way™ of managing your python environment these days.Arthropod
Where do you get the "allergic" from?Lawless
There are all sorts of places where you don't want/need a virtual environment. The most common being inside of a Docker container.Grapeshot
@Grapeshot I would argue that if you're using a Docker container, you probably don't have the OP's original problem (how to avoid installing a global Python module).Arthropod
I
21

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

Inferno answered 13/6, 2018 at 14:49 Comment(0)
P
9

you should install a virtual python environment so you don't have to ever worry about having admin privileges, and you can install whatever you want.

Planimeter answered 30/1, 2012 at 5:31 Comment(2)
That may not be an option on all builds at all times. Consider if someone were deploying their libraries to a different platform; would you mandate that they also have this installed?Seibel
+1 Not a complete solution, but a great one for most use cases.Lout
R
9

Also if you want to use it frequently like in script . Easy way to export "PYTHONPATH" in bashrc/zshrc file and give path to the directory containing your code .

For example:

export PYTHONPATH=$PYTHONPATH:$HOME/path/to/module

This way you don't need to do sys.path everytime you restart .

Happy Coding

Ruler answered 28/10, 2014 at 19:8 Comment(0)
U
2

Option 1: venv

Just create a virtual environment with venv and install it in that:

$ python -m venv venv
$ source venv/bin/activate

# Form here on, pip is the pip from venv!
$ pip --version
pip 20.3 from /home/math/venv/lib/python3.8/site-packages/pip (python 3.8)
$ pip install tweepy

Option 2: --user

Install the package in the user directory with --user

$ pip install tweepy --user
Unmake answered 3/3, 2021 at 10:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.