How to Make My Python Module Available System Wide on Linux

How to make my Python module available system wide on Linux?

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.

How can I create a simple system wide python library?

You're trying to make a module.

Start by installing the setuptools package; on either Windows or Linux you should be able to type pip install setuptools at a terminal to get that installed. You should now be able to write import setuptools at a python prompt without getting an error.

Once that's working, set up a directory structure containing a setup.py and a folder for your project's code to go in. The directory must contain a file called __init__.py, which allows you to import the directory as though it's a file.

some_folder/
| setup.py
| my_project/__init__.py

In setup.py, drop the following content:

# setup.py
from setuptools import setup

setup(name="My Awesome Project",
version="0.0",
packages=["my_project"])

In my_project/__init__.py, drop some stuff that you'd like to be able to import. Let's say...

# my_project/__init__.py
greeting = "Hello world!"

Now, in order to install the project at a system-wide level, run python setup.py install. Note that you'll need to run this as root if you're on Linux, since you're making changes to the system-wide python libraries.

After this, you should be able to run python from any directory you like and type:

>>> from my_project import greeting
>>> print greeting
Hello world!
>>>

Note that this is enough to tell you how to make a module, but there's one hell of a lot of stuff that setuptools can do for you. Take a look at https://pythonhosted.org/setuptools/setuptools.html for more info on building stuff, and https://docs.python.org/2/tutorial/modules.html for more info on how modules actually work. If you'd like to look at a package that (I hope) is reasonably simple, then I made my LazyLog module a couple of weeks ago on a train, and you're welcome to use it for reference.

How to give python access to system wide modules in ubuntu?

I think the root cause is you have several python binary under $PATH, and your system doesn't use /usr/bin/python by default.

  1. run command which python to see which python is used by default
  2. rename the default python file to something like 'python-2-7-12'

then try to run python test.py again to see if it is resolved.

How to have python functions and classes available system-wide with an easy `import` statement

You completely misread the sources you were working off of. If you want to be able to import SmartDisk directly from the mausy5043libs package, then mausy5043libs/__init__.py needs to import SmartDisk:

# in mausy5043libs/__init__.py
from .libsmart3 import SmartDisk

Then users of the package can import it the way you want:

# in code that wants to use SmartDisk
from mausy5043libs import SmartDisk

The article you linked includes this information under "What goes in __init__.py?", although they use pre-Python 3 implicit relative import syntax.

The reason cause different location of python packages

Your question is mainly about the Linux filesystem layout. You can read a lot about that, for example on Wikipedia and more specific for the different lib locations in this askubuntu question.

I'll try to answer (1) and (2) by summarizing how the three given folders are conventionally used:

  • /usr/lib/python3/dist-packages contains non-host-specific modules installed by the system with the package manager, for example on Ubuntu with sudo apt-get install python-numpy.

  • /usr/local/lib/python3.6/dist-packages contains modules that you installed yourself system-wide through a package manager, for example with sudo pip install numpy. (Of cause using sudo pip can cause problems as you rightly mentioned.)

  • /home/twotwo/.local/lib/python3.6/site-packages contains modules that the user twotwo has installed in his own user directory, for example by using pip in user-mode. Those modules can of cause only be imported by twotwo, because they don't appear in other user's PATH variables and might not even be readable by another user.

Also note that the naming dist-packages is a Debian (and derivates) specific convention for python packages installed using debian packages, as explained here. In a manual python installation, the respective folders would be named site-packages, as is standard for pip.

As to question (3): The details about this can be read in the Python 3 docs. Basically, after looking for the module in the folder from which your python script is run, the folders in your sys.path variable are looked up in the same order in which they are listed there. As soon as a module of matching name is found, it is imported.



Related Topics



Leave a reply



Submit