3

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!

3
  • Thank you all for your answers. You pointed me to the meaning of __init__.py. That's exactly what I needed: now I add __init__.py to every subfolder of my working folder from which I want to import. All my code is exactly where I want it, and easily importable. Great help, thanks! Commented Apr 7, 2011 at 20:14
  • P.S. Your scripting is programming. :-) Keep it up and you'll be answering questions here before you know it. Commented Apr 7, 2011 at 20:58
  • Thanks Kirk. You're right that things can change quickly, especially with all these experts helping me out :-) Commented Apr 7, 2011 at 21:54

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

You write: There's a link from /usr/local/lib/python2.7/site-packages/ to it. I'm working on windows. Would a shortcut work?
I have no idea. If not, you could probably work directly from that folder, and make a nice desktop shortcut to it so you can find it easily. The arrangement is slightly different on servers, but if you can get it sorted out on your workstation, you can figure out the server part if/when the time comes.
Depending on your windows version (starting with Windows Vista) you can also use symlinks: howtogeek.com/howto/windows-vista/…
2

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

3 Comments

Thanks for your answer. These are linux paths, right? And when i would install a new python version, the modules in .../site-packagesare not overwritten?
Yes, these are linux paths. Modules in these places will not be overwritten, as core Python has no modules in any path containing local. Apart from that, these two paths are version agnostic (note the X.Y part in both paths).
That's not true for all Linux distros, and definitely not on FreeBSD (where Python goes into /usr/local) and some OS X package managers (where it lives in /opt). The part about site-packages being left alone is correct, though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.