1

I'm a complete newbie to this so bear with me; the only thing I've done with packages is let anaconda install numpy for me, and then I import it (and some others) into scripts. This is the first time I play with a package itself.

The goal: I have a bunch of .py scripts that I would like to collect into a package; I want the behaviour I'm used to; I have python programs that live somewhere in my computer (wherever someone chose to save a project) and without having to worry about path (because that's a can of worms I don't understand either), I want to type, mypackage, "import mypackage as mp" (just like import numpy as np), and then be off to the races.

Note that I don't know how numpy or any proper package is structured so maybe the analogy is misleading? But what I do want is to import functions defined across multiple .py scripts that are all saved in a single folder (which for convenience I also call mypackage).

I followed the example in this video, I wrote a module and placed it in anaconda3\Lib\site-packages\ and was able to write a python program that lives in some random other folder which is able to then import and run the module.

But, again, that was only allowing me to access functions corresponding to a single .py script/module. I would like to be able to add a folder (I'm assuming with address anaconda3\Lib\site-packages) with all of my scripts/modules, then be able to import a package corresponding to all those modules.

I've tried some solutions, but no luck yet. I don't really understand the init.py and path.py scripts (specifically, I don't understand if they need to be in \site-packages\ or in \site-packages\mypackage). In particular, I followed this solution because he spells it out, but I just can't get python to import anything within \site-packages\mypackage.

Edit: I followed the steps in this video and it got me the results I wanted.

Package folder structure

Subpackage folder structure

However, not all the ways used to import the packages in the video work me. Here is the code that I was able to use successfully in a python script that lives, for example, on my desktop and is able to access the packages as follows:

import mypackage.demo # mypackage is the folder, demo is the python script
mypackage.demo.demoprint() # demoprint() is the function defined in demo.py
> Now in the demo file!
del mypackage

from mypackage.demo import demoprint
demoprint()
> Now in the demo file!
del demoprint

import mypackage.demo as mp
mp.demoprint() 
> Now in the demo file!
del mp

# Call subpackage functions

from mypackage.subpackage.subdemo import subdemoprint
subdemoprint()
> Now in the subdemo package and file!
del subdemoprint

from mypackage.subpackage import subdemo as sd
sd.subdemoprint()
> Now in the subdemo package and file!
del sd
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Francisco C
  • 193
  • 3
  • 7
  • There are a ton of questions on here about python packages and importing (which tells you that it is a sore point that confuses many). You are going to have to bite the bullet and read the docs. In the meantime just put the scripts in the same folder. – Ywapom May 24 '18 at 22:06

0 Answers0