Reputation:
I see there are some built in packages that I can import from any script like:
from datetime import date
today = date.today()
print today
How can I create a simple package and add it to the system library so that I can import it like datetime
in the above example?
Upvotes: 10
Views: 8151
Reputation: 24788
If you simply want the module for personal use, just drop it in a folder and add that folder to the PYTHONPATH
environment variable.
For example, create a folder in your home directory called ~/python-packages
, then add the following line to your .bashrc
(assuming you are using bash):
export PYTHONPATH=$HOME/python-packages`
Then, simply drop any modules/packages you want to make available in ~/python-packages
.
Upvotes: 1
Reputation: 7840
The quick way, if you're just making something for your own use and not worrying about packaging, is to put the module (which could be as simple as a single file) in your system's site-packages
directory. (On Debian-based systems, you probably want to use dist-packages
instead).
To find out where your site-packages
/dist-packages
directory is, start Python and:
>>> from sys import path
>>> path
['', '/usr/lib/python3.4/site-packages/pip-7.1.2-py3.4.egg', '/usr/lib/python34.zip', '/usr/lib/python3.4', '/usr/lib/python3.4/plat-cygwin', '/usr/lib/python3.4/lib-dynload', '/usr/lib/python3.4/site-packages']
Note the last item in that example: /usr/lib/python3.4/site-packages
. That's the sort of thing you're looking for. So in this example, if I save the following to /usr/lib/python3.4/site-packages/foo.py
:
def bar():
print('Hello world!')
Then from anywhere on my system:
>>> from foo import bar
>>> bar()
Hello world!
Upvotes: 7
Reputation: 1666
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.
Upvotes: 15
Reputation: 2990
If you really need a package, you can do it with boost, which allows interacts with C++. You may implement algorithms using C++ and compile it as a Python lib. However it's poorly documented. And as doc described, C API should be a basic option. Boost is built on C API any way.
Sample: I made it several years ago on a class. You can do: import tfidf
.
Upvotes: 1
Reputation: 29
Add you python script or package's path to sys.path or just move them to one of the location in sys.path. BUT i do not suggest to do this...
Upvotes: -2