1

I am working on a little video game in Pygame. I want it to be very clear in the folder hierarchy, so, I've already prepare it in this way :

Python 
   Project 
      bin
        init.py
        Project.vbs
      lib
        constants.py
        definitions.py
      sprites ( useless for topic )
      Project.exe

The Project.exe is a ink file, a fake executable. In fact, it's a shortcut to the Project.vbs with open the init.py (It's just for have the clearest folder managment).

What is my problem? I want to import the difinitions.py and the constants.py from the init.py which is in 'bin' folder, it's just absolutely critical for the game. By The way, the files are saved on my USB key but the path of this one always change:

On my own computer, it's C:/user/Save19/Python/...

On my high school computers, it's P:/documents/Python ( internship )

On my phone it's /storage/0/Python/...

And anytime I made a copy, the path change...

So I've read a lot of topics for try to fix that but anytime it's not working :/

I've try it by using :

Import constants from lib import constants from lib.definitions import * I've try ths with the os and the path

import sys sys.path.insert(0, 'Project/lib') import constants

import sys sys.path.append('Project/lib') import constants

But it not working anyway... Can somebody give me a solution and explain me it?

  • Possible duplicate of [Python3 Importing module/package from sibling directories](https://stackoverflow.com/questions/43225775/python3-importing-module-package-from-sibling-directories) – Paritosh Singh Jan 30 '19 at 19:28
  • No, it isn't because I've already try to add somes '__init.py__' to all the folders and still not working... –  Jan 30 '19 at 19:31
  • after adding the init files, have you tried it with the relative import as mentioned in the answer there? – Paritosh Singh Jan 30 '19 at 19:34
  • It's said 'constant' molude not existing... –  Jan 30 '19 at 19:40
  • check spelling. constants??? – Paritosh Singh Jan 30 '19 at 19:41
  • @Edhyjox: *"to import `*.py` from the `init.py` which is in `bin` folder"*: The name should be `__init__.py` and should be placed in `lib`. Your `sys.path.append('Project/lib')` should be `sys.path.append('./lib')` – stovfl Jan 30 '19 at 19:42
  • @ParitoshSingh Yeah, It's just a mistake in my answer xD –  Jan 30 '19 at 19:51
  • @stovfl It's continu to said me `line 9, in import constants ModuleNotFoundError: No module named 'constant'` –  Jan 30 '19 at 20:14
  • @Edhyjox: I use relative path, is your `cwd == Python/Project`? – stovfl Jan 30 '19 at 20:21
  • @stovfl Wait, what is cwd ? –  Jan 30 '19 at 20:23
  • @Edhyjox: Current Working Directory – stovfl Jan 30 '19 at 20:24
  • Yes, it's that ( there is only two others files that I haven't mentioned ( 1st named 'Font', and second named 'sprites' with respectively somes custom fonts and all the picture I need ) –  Jan 30 '19 at 20:27

1 Answers1

0

It really depends from where you're starting the Python process. Whatever path you're adding should be relative to that location. So you say it's started from within bin and so you can add

import os
import sys

sys.path.append(os.path.abspath('../lib'))
import constants

You can also add __init__.py to lib and then add ".." to path and do import lib.constants as constants.

Or you can setup and install the whole thing as a Python package and then use absolute imports such as import mygame.lib.constants as constants.

a_guest
  • 34,165
  • 12
  • 64
  • 118
  • Thank a lot : Working with `import os import sys sys.path.append(os.path.abspath('../lib')) import constants` –  Jan 30 '19 at 20:38
  • Please vote for my question if this one help you, It's help me to won some reputation ! :D –  Jan 30 '19 at 20:46