Custon Libraries
Author |
Message |
Nathan4102
|
Posted: 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 |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Zren
![](http://compsci.ca/v3/uploads/user_avatars/1110053965512db6185954b.png)
|
Posted: 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 |
|
|
|
|
![](images/spacer.gif) |
Nathan4102
|
Posted: Mon Apr 01, 2013 3:59 pm Post subject: RE:Custon Libraries |
|
|
Awesome, thanks! ![Smile Smile](http://compsci.ca/v3/images/smiles/icon_smile.gif) |
|
|
|
|
![](images/spacer.gif) |
Cancer Sol
![](http://compsci.ca/v3/uploads/user_avatars/15559360865161dd9d48ddd.gif)
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
DemonWasp
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
Cancer Sol
![](http://compsci.ca/v3/uploads/user_avatars/15559360865161dd9d48ddd.gif)
|
Posted: 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
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 :/ |
|
|
|
|
![](images/spacer.gif) |
|
|