Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Custon Libraries
Index -> Programming, Python -> Python Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Nathan4102




PostPosted: Mon Apr 01, 2013 10:16 am   Post subject: Custon Libraries

I've made a python program containing a bunch of functions I will use in the future for a project I'll be creating. How would I attatch this to my future programs without actually pasting all the code to the top of them all, like a library? I'm using WING IDE if it matters

Thanks,
Nathan
Sponsor
Sponsor
Sponsor
sponsor
Zren




PostPosted: Mon Apr 01, 2013 3:32 pm   Post subject: RE:Custon Libraries

You're going to have to make a module.

Your main.py is a module unto itself. It has it's own namespace. You can make another file in the same directory called utils.py where you can put functions you tend to reuse. You can then use either:

Python:

import utils
utils.prettyprint(utils.my_var)


OR

Python:

from utils import prettyprint, my_var
prettyprint(my_var)


You can also create a full blown package where you have multiple files inside a folder.

With both these module files/packages, you still have to copy the file/folder into your new project location if they don't share the same folder (as the project root directory would be different).

I've never done this, but I think you can add a folder to the PYTHONPATH environment variable which contains your own module files/package. In theory, this should allow you to import those modules without explicitly copying them into your projects root folder.

More on modules can be found in the documentation: http://docs.python.org/2/tutorial/modules.html
Nathan4102




PostPosted: Mon Apr 01, 2013 3:59 pm   Post subject: RE:Custon Libraries

Awesome, thanks! Smile
Cancer Sol




PostPosted: Tue Apr 02, 2013 1:13 am   Post subject: Re: Custon Libraries

Just wondering, how do I do this for C++?
I use Code::Blocks.

I think I've seen how to do this in the ebook I have, but I don't want to go searching for the section.
DemonWasp




PostPosted: Tue Apr 02, 2013 1:42 am   Post subject: RE:Custon Libraries

The equivalent of the above in C++ would be to put your .cc (or .cpp or .C or ...) and .h (or .hpp or ...) files in your project's source (src) directory, then use something like:

code:

#include "any/dir/MyClass.h"


Then you need "include guards" in your MyClass.h file, which you can do with #pragma once or the older #ifndef/#define/#endif pattern.

To actually use proper libraries (either static- or dynamically-linked) is considerably more complicated and different by operating system, though the ideas are the same.
Cancer Sol




PostPosted: Tue Apr 02, 2013 2:12 am   Post subject: Re: RE:Custon Libraries

[quote="DemonWasp @ 4/2/2013, 1:42 am"]The equivalent of the above in C++ would be to put your .cc (or .cpp or .C or ...) and .h (or .hpp or ...) files in your project's source (src) directory, then use something like:

code:

#include "any/dir/MyClass.h"


Then you need "include guards" in your MyClass.h file, which you can do with #pragma once or the older #ifndef/#define/#endif pattern.

To actually use proper libraries (either static- or dynamically-linked) is considerably more complicated and different by operating system, though the ideas are the same.[/quote
Thanks. That's kinda confusing, so I'll just search it up on the ebook again Razz
So I'll have to include it in every project then? The way you showed it? Idk, I don't really understand it too much :/
Display posts from previous:   
   Index -> Programming, Python -> Python Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: