14

I made myself a little module which I happen to use quite a lot. Whenever I need it I simply copy it to the folder in which I want to use it. Since I am lazy I wanted to install it so that I can call it from anywhere, even the interactive prompt. So I read a bit about installing here, and concluded I needed to copy the file over to /usr/local/lib/python2.7/site-packages. That however, doesn't seem to do anything.

Does anybody know where I need to copy my module for it to work system wide?

kramer65
  • 50,427
  • 120
  • 308
  • 488
  • 1
    See the [site module documentation](http://docs.python.org/2/library/site.html#module-site). You many need to add a `.pth` path configuration file to one of the directories. – martineau Jun 21 '13 at 14:44

6 Answers6

14

There are methods to install Python modules system-wide. You may want to take a look at distutils. A good tutorial for distutils2 (the current version) can be found here.

You basically have to write a file setup.py which tells distutils what to do. Then you can simply

python setup.py install

with root permissions to install your module systemwide. There are good and easy examples, plus it's the cleanest way I can imagine.

Johannes P
  • 888
  • 8
  • 16
  • I already saw this option, but I thought it wouldn't be needed to create this setup script if I could simply do a 'mv mymodule.py /some/directory'.. Isn't there an easy way without creating this whole setup script? – kramer65 Jun 21 '13 at 13:57
  • Probably it really is enough to copy it to some directory which is contained in sys.path, as you already tried. However, there are some other things you need to take care of (like file permissions, did you check them?) and maybe you need to put a reference to your module somewhere else (I don't know anything about that). – Johannes P Jun 21 '13 at 14:10
  • So, how to make `python setup.py install` to copy code to `site-packages`? – Dims Oct 27 '17 at 09:07
5

The answer is: it's all about permissions.

It's not enough to place the file in the correct location, like, such instance: /usr/local/lib/python2.7/dist-packages, you also need to ensure that the file can be read by the process you're running, in this case, python.

Be sure that "other" users have read access to the file. Open the bash console and execute this:

sudo chmod o+r "yourmodule.py"
[Introduce the password]

After this go again to python and try the import:

import "yourmodule"

As long as the path where the .py file is located is present in PYTHONPATH + the file is readable then you should be allowed to import it.

cSn
  • 2,796
  • 3
  • 23
  • 29
4

In one of the directories listed when you type sys.path in your Python prompt. You can also add the directory which contains your file by modifiying the PYTHONPATH environment variable:

# ~/.bashrc file
export PYTHONPATH+=:/some/dir
michaelmeyer
  • 7,985
  • 7
  • 30
  • 36
  • /usr/lib/python2.7/dist-packages is also in sys.path, but when I move my file there and try to import it from the interactive python prompt I still get an ImportError.. – kramer65 Jun 21 '13 at 13:55
  • The file name is mymodule.py and has exectuable permissions. On the interactive Python command line I simply type "import mymodule". Could there be anything wrong with that? – kramer65 Jun 22 '13 at 10:57
  • Either your install is broken, or you're trying to import the module from an other version of python – michaelmeyer Jun 22 '13 at 11:08
2

If you're using Ubuntu, copy files to /usr/local/lib/python2.7/dist-packages. Following command will show you where to copy.

python -c "from distutils.sysconfig import *; print(get_python_lib())"

If you are the only one use the module, copy files to ~/.local/lib/python2.7/site-packages.

falsetru
  • 357,413
  • 63
  • 732
  • 636
  • That command gave me /usr/lib/python2.7/dist-packages, so I moved my file there. When I now enter the Python interactive prompt and try to import mymodule, I still get an ImportError.. – kramer65 Jun 21 '13 at 13:53
  • This is the right answer. you can put a symlink to the location output here and it will work immediately. – v4gil Oct 05 '17 at 22:32
0

Couple of things.

First the module must, (I believe), be in a directory that matches the module name.

Put that module directory under one of the directories in the PYTHONPATH (I use /usr/lib/pymodules/pythonV.x/). You can find a suitable directory in the path using

import sys
print(sys.path)

from the python prompt.

Michael Currie
  • 13,721
  • 9
  • 42
  • 58
Brian
  • 349
  • 3
  • 17
0

You could also have a folder that contains all your global modules e.g. my_modules/ then localize the site-packages/ folder of the Python version your are using (you will find it easily by using this command: python -m site). Now, cd to this folder and create a custom file with a .pth extension. In this file, add the absolute path to the folder that contains all the modules you want to make available system wide and save it. Your module should be available now

snoob dogg
  • 2,491
  • 3
  • 31
  • 54