Reputation: 10129
As a beginning programmer (I'm rather doing scripting), I'm struggling with my growing collection of scripts and modules.
Currently, small scripts are in a general folder that is added to my pythonpath. Specific projects get their own subfolder, but more and more I try to write the general parts of those projects as modules that can be used by other projects. But I cannot just import them unless this subfolder is also in my pythonpath.
I don't know how to organise all this. I will be happy to get your hints and recommendations on organising (python) code. Thanks!
Upvotes: 3
Views: 486
Reputation: 30977
Be sure to read about Packages, specifically the part about creating a file named __init__.py
in directories you want to import from.
I have a git repository named MyCompany
in my home directory. There's a link from /usr/local/lib/python2.7/site-packages/
to it. Inside MyCompany
are package1
, package2
, package3
, etc. In my code, I write import MyCompany.package1.modulefoo
. Python looks in site-packages
and finds MyCompany
. Then it finds the package1
subdirectory with an __init__.py
file in it - yay, a package! Then it imports the modulefoo.py
file in that directory, and I'm off and running.
Upvotes: 2
Reputation: 2100
General-purpose custom modules should go to ~/.local/lib/pythonX.Y/site-packages
, or to /usr/local/lib/pythonX.Y/site-packages
if they should be available to everyone. Both paths are automatically available in your $PYTHONPATH
. (The former is available since Python 2.6 – see PEP 370 – Per user site-packages directory.)
Upvotes: 2