Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install python modules in a local directory? --user and exporting pythonpath isn't working

I don't know very much about python but would like to install some python modules in a local directory on a server on which I don't have sudo access.

I start by going into my desired directory (not root) and create the directory tree needed to store my custom modules

cd /root/example/sub-example
mkdir -p local/lib/python2.7/site-packages

I then export this local path to PYTHONPATH

export PYTHONPATH=$PYTHONPATH:/root/example/sub-example/local/lib/python2.7/site-packages

I then make a new sub-directory to store the python package while extracting

mkdir example-python-directory
cd example-python-directory
wget http://example-python-package
tar -xvf example-python-package.tar.gz
cd example-python-package

Last, I run the setup.py script with the --user flag to try to get it to install in my specified /local directory

python setup.py install --user

The problem is, nothing is installed in my /root/example/sub-example/local/lib/python2.7/site-packages directory, and instead I find that I now have a new directory at root: /root/.local/lib/python2.7/site-packages

Is there a way to prevent this? I feel like my lack of Python knowledge is causing me to make some silly error that is probably obvious to others. Thanks for the help!

like image 280
jake9115 Avatar asked Mar 14 '15 03:03

jake9115


People also ask

How do I install Python modules locally?

Installing Python Packages with Setup.py 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.

Why can't Python find my modules?

This is caused by the fact that the version of Python you're running your script with is not configured to search for modules where you've installed them. This happens when you use the wrong installation of pip to install packages.


2 Answers

create a folder called "lib"

For Python 3

pip3 install <your_python_module_name> -t lib/

For Python 2

pip install <your_python_module_name> -t lib/
like image 106
Thamira Madusanka Avatar answered Sep 23 '22 18:09

Thamira Madusanka


CFLAGS=-I$(brew --prefix)/include LDFLAGS=-L$(brew --prefix)/lib pip install <package>

I found that on servers where you haven't root access to, you can usually install the python module into your .brew/lib using this.

like image 44
Aaron Derby Avatar answered Sep 25 '22 18:09

Aaron Derby