1

Im wondering: I have a library of python scripts. Now i want to share a particular script with someone and not give them my whole library, and obviously it has some requirements, so I'm wondering, how do you create a 'package' kinda thing? (not familiar with the lingo, never needed it. apologies if it means something else to you...)

  • say i have a fileA.py, which imports fileB.py and fileC.py. but fileB.py also imports fileN and fileG.
  • is there a way to create a new package, that would contain a structure of files only needed by that initial file, and even better - delete functions from files B, C, G, N, that aren't required?

There must be something like that out there, i can't be the only person needing this...

Thanks in advance,

Pete

Pete
  • 41
  • 1
  • 4

1 Answers1

0

Managing packages is one of the biggest challenges when using python. There are many package managers such as pip that help with this, but I recommend using conda. A package manager such as miniconda is great for sharing python environments.

You create an environment with you requirements (e.g. Pandas). You can then export the exact package requirements that you are using.

Once conda is installed, you'd type this from the terminal to install Pandas and any required dependency such as python and numpy.

$ conda create -n my_new_environment pandas

You then activate your environment, launch python to do all your testing, debugging, etc. To list the exact versions of all your modules, type this from the terminal:

$ conda list -e

Typing the following will create a file containing this list in the current working directory.

$ conda list -e > my_env_requirements.txt 

The other user would then have to ensure that there package requirements are identical. If they are using conda, they can easily create a new environment with your file.

$ conda create --name my_friends_environment --file my_env_requirements.txt

I've recently answered a similar post here that covers some related material:

my jupyter notebook fails to import anaconda modules consistently

Community
  • 1
  • 1
Alexander
  • 105,104
  • 32
  • 201
  • 196
  • massive thanks for your reply mate, much appreciated. I am not going through terminal or python per se, im only ever working with Python through Maya. which might be a challenge for me on its own. Plus the idea was to package this one script, and pass it on to whoever without them needing to install anything else but to move that folder into their designated python path. sounds like if i want to have that done today and avoid any potential issues, i better do it by hand, huh? :/ – Pete Mar 09 '16 at 06:15
  • It really depends on which dependencies you have (i.e. which non standard python modules) and how critical your code is. It may simply be enough to say use this version of module X on python 2.7, and let them figure out how to install the relevant module(s). The dependencies for the scientific stack e.g. Numpy, Scipy and Pandas, can get very complicated very quickly. – Alexander Mar 09 '16 at 06:46
  • honestly doesnt have anything specific like that. but im a 3D artist. my library means having file name.py and in there a ton of functions dealing with name formatting and whatnot. then a file called attr.py with a ton of functions dealing with attributes on the nodes in Maya. so all the file in question needs is literally just a few functions from this file, few functions from this file, etc... it doesn't have any dependencies outside of my own scripts. – Pete Mar 09 '16 at 07:13
  • You don't have any imports of 3rd party modules? – Alexander Mar 09 '16 at 07:14
  • When you import a module, you import *everything* in it (even if you only import one function from the module). Perhaps the docs are a good place to start. They discuss Modules and Packages. https://docs.python.org/2/tutorial/modules.html – Alexander Mar 09 '16 at 07:21
  • thanks, i appreciate the words. I've been through the docs. And no, i don't use any 3rd party modules. dont need anything of that sort. i know it may be hard to understand without diving into what i do, which i can't. Needless to say, i do 'import repo.name as cs_name' kind of imports. to keep the namespace and to be able to use my daily-usage functions. when i have 20 functions in the name.py file and i know im gonna use 10 or 18 of them, i don't see a reason to import one by one, if that's what you're asking? – Pete Mar 09 '16 at 08:36