Imports in Python 3

Asked

Viewed 107 times

0

Hello! I’m having a very recurring problem regarding "custom" module Imports in python 3 I’m doing a project of a very basic game thing myself and so far this is the structure of my project:

game/
   __init__.py
   main.py
   classes/
       __init__.py
       player.py
   media/
   scripts/
       __init__.py
       dices.py

The problem comes when in the file "player.py" in the folder "classes" to do critical damage to the player it uses a function that is in the folder "functions/Dices.py"

If I try to use "from scripts import Dices" gives the "Import: cannot import name 'Dices' from 'scripts' (Unknown Location)"

If I use an import relative "from .. import Dices scripts" gives the "Import: attempted relative import with no known Parent package""

I have tried several ways including I saw answers in another discussion about which in the PEP 420 the archives init.py are no longer required (as far as I understand the comment) this is the link "/questions/371871/como-scri-um-arquivo-init-em-python-3"

I don’t understand anything else! If you can help me I will be very grateful!

  • try to put: import ../functions/dice

  • From the syntax error "import .. /functions/Dice Syntaxerror: invalid syntax"

  • you use linux or windows? if it’s windows I guess changing / for \ works

  • Use windows, in case it is to replace the "/" bar by double crase? "``"?

  • Wow, I didn’t realize the comment went wrong, sorry, subistitua / for \

  • Also not import .. functions Dices Syntaxerror: invalid syntax The syntax error points to two points ".."

  • from ..scripts import dices, why scripts, if the folder name is functions?

  • @Eliasoliveira see if the answer to this question helps you: https://answall.com/questions/427965/import-em-directors-different-em-python3/427966#427966

  • @Woss was typing error (here in the thread) the folder name is scripts, mals by vacilo

Show 5 more comments

2 answers

1


Solved! I was able to identify the problem by looking for the path that the interpreter was doing, just import the sys module and use the sys.path method that lists all directories that the interpreter will look for. When doing this I did not find the directory of the scripts only the root folder of the project. The solution to this is very simple:

import sys
sys.path.append('D:\...\...\game v3 (stable)\scripts')

Where the '...' represent the path to the folder with the scripts, when doing so in main.py I had access to the scripts directory

  • Attention all who will use this, it has solved my problem on the local machine, but in production environments, or in other machines will not work since it is referring to the absolute path in reference to my machine, the best alternative is to invest enough time to think about the structure of the project.

0

According to official documentation, when you want import some local file, just put: import nomeDoArquivo, but when it is in a different folder than the local, the python uses the . envés da / or .

With that his import would look like this:

from scripts.dices import *

# seu código aqui em baixo

Or

import scripts.dices

Browser other questions tagged

You are not signed in. Login or sign up in order to post.