Making Python Script Accessible System Wide

Making Python script accessible system wide

If your script starts with a suitable shebang line, such as:

#!/usr/bin/env python

And your script has the executable bit set (for Linux, OS X, and other Unix-like systems):

chmod +x myscript.py

And the path to your script is in your PATH environment variable:

export PATH=${PATH}:`pwd` # on Unix-like systems

SET PATH=%PATH%;\path\to # on Windows

Then you can call myscript.py from wherever you are.

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

How can I make a Python script standalone executable to run without ANY dependency?

You can use py2exe as already answered and use Cython to convert your key .py files in .pyc, C compiled files, like .dll in Windows and .so on Linux.

It is much harder to revert than common .pyo and .pyc files (and also gain in performance!).

System-wide mutex in Python on Linux

The "traditional" Unix answer is to use file locks. You can use lockf(3) to lock sections of a file so that other processes can't edit it; a very common abuse is to use this as a mutex between processes. The python equivalent is fcntl.lockf.

Traditionally you write the PID of the locking process into the lock file, so that deadlocks due to processes dying while holding the lock are identifiable and fixable.

This gets you what you want, since your lock is in a global namespace (the filesystem) and accessible to all processes. This approach also has the perk that non-Python programs can participate in your locking. The downside is that you need a place for this lock file to live; also, some filesystems don't actually lock correctly, so there's a risk that it will silently fail to achieve exclusion. You win some, you lose some.

How do I make environment variable changes stick in Python?

Under Windows it's possible for you to make changes to environment variables persistent via the registry with this recipe, though it seems like overkill.

To echo Brian's question, what are you trying to accomplish? There is probably an easier way.



Related Topics



Leave a reply



Submit