0

Anyone looking for the answer to this question: This answer worked perfectly!

I am making a program that will store X and Y coordinates for positioning on a target. I will then use it to calculate average shot position for a series, etc..

When I first started making the program I had all files in the same folder I opened in VSCode, which worked. It got out of hand and I put the files into categorized folders and started reading about packages. I can't get them to work though.

I've read answers to questions on StackOverflow, I've used examples from other sites (which don't work for me), I've even used the Python documentation which hasn't worked.

Things I've done:

  • Updated my Python version from 3.7.4 32-bit to 3.9.1 64-bit
  • Tested line for line this example (What doesn't work is importing from higher files into a lower one and Py-lint tells me that importing security.py from init.py in the MyPackage directory shouldn't work yet it does)

My code and file structure:

myPackage\
       security.py
       subfolder1\
                item.py
                subfolder2\
                        student.py
                        user.py                                   
       subfolder3\
                 accounts.py
                 registration.py     
#from __init__.py in myPackage
import security
from subfolder1.subfolder2 import user

lol = security.lol()
print (lol)

x = user.testing()
print(x)
  • Tested this example from Python docs which doesn't work at all for me.
test\
     program.py
     package\
            subpackage\
                      lol.py                                
            subpackage2\   
#from program.py
import package.subpackage.lol

x = lol.test1()
print(x)
  • And of course tested all sorts of examples here on SO

I have no idea how sys works or how path works so solutions of that kind need a little explanation and are not really desirable.

Honestly run out of ideas at this point. The file structure of my own program is a little big and there is too much code to get into in my opinion. If anyone knows how to fix these issues and can explain how inter-package imports work (one file in a package to another in the same package) like in the Python docs, how to import from a higher file to a lower, and from a lower file into a file inside the package I would be more than happy.

Tall order I know! I can't find these answers and the answers I do find don't work, thank you for any help you can provide!

Gixty
  • 1
  • 2
  • Maybe read this: https://sinoroc.gitlab.io/kb/python/python_imports.html -- What do you want your top level packages and/or modules to be? I would venture to say you want one and only one top-level package: `myPackage`. So make all your imports absolute accordingly, this way it works from anywhere, for example: `from myPackage import security`, `from myPackage.subfolder1.subfolder2 import student`. – sinoroc Jan 04 '21 at 18:08
  • @sinoroc The myPackage would contain all of the necessary functions and code for the main program. I can import what I need into one file that controls the system. The problem is that sometimes I want to use a function that exists inside myPackage but in a different folder. So if I'm in `subfolder3` and want to access something in `subfolder2` or `subfolder1` how would I do that, or should I restructure everything to avoid needing to do that? I tested you import code and worked just fine. – Gixty Jan 05 '21 at 13:39
  • I do not understand your question. You just wrote: "_I tested you import code and worked just fine_", so what is the problem? – sinoroc Jan 05 '21 at 14:32
  • @sinoroc I may have misunderstood you. I tested that import code you commented and it worked, however it works only when outside of `myPackage`. My question is: "_The problem is that sometimes I want to use a function that exists inside `myPackage` but in a different folder. So if I'm in subfolder3 and want to access something in subfolder2 or subfolder1 how would I do that, or should I restructure everything to avoid needing to do that?_" – Gixty Jan 05 '21 at 14:50
  • Interestingly in my project when I import from outside of the package I receive a pylint error of "Unable to import" yet it works anyways. In the `myPackage` example I don't receive this error message. – Gixty Jan 05 '21 at 14:54
  • This should always work, no matter what. Did you follow the suggestions [here](https://sinoroc.gitlab.io/kb/python/python_imports.html)? Did you install you project correctly, or at least are you sure you are in the right directory? – sinoroc Jan 05 '21 at 15:16
  • I use VScode and when I'm working uing the package `myPackage` I'm not opening it as a folder. I open the folder that contains it. So it would be: "testing\myPackage" and I open testing as a folder. When in `student.py` I receive error message: `from myPackage import security ModuleNotFoundError: No module named 'myPackage'` – Gixty Jan 05 '21 at 15:23
  • @sinoroc I also don't know what "_make your project a real installable project, so that those top level modules and packages are installed in the environment’s site-packages directory;_" means and I don't know how to use the _executable module_ method. Do I use that instead of `import`? – Gixty Jan 05 '21 at 15:25
  • Do not open folders or anythin, stay at the root of your project. I have no idea how VScode works, you have to figure this out with them. -- Make it installable, means adding a correct `setup.py`. -- The _module_ method means: call `python -m package.module` instead of calling `python package/module.py`, there is a huge difference. – sinoroc Jan 05 '21 at 17:10
  • You may also want to read this answer: https://stackoverflow.com/a/50193944/11138259 -- You will need to figure some things out. It is impossible to answer your question, because you do not have 1 question but dozens of questions. It feels like you are out of your depth. I would recommend you to go step by step, play around with a simple project until you get a better understanding of the basic concepts. – sinoroc Jan 05 '21 at 17:12
  • @sinoroc I will try this out, I have the feeling this is exactly what I've been looking for! I've done simpler projects in the past, this is my first larger project using imports of this kind. Thank you for your help! – Gixty Jan 06 '21 at 10:26
  • 1
    [This](https://stackoverflow.com/questions/6323860/sibling-package-imports/50193944#50193944) answer worked perfectly! – Gixty Jan 08 '21 at 20:31
  • Does this answer your question? [Sibling package imports](https://stackoverflow.com/questions/6323860/sibling-package-imports) – sinoroc Jan 08 '21 at 22:35

0 Answers0