1

Hello I look into the forum and find a lot of people who had this problem but the solutions were not successful for me!

I have one main file: "PythonApplication1.py" in the directory : "PythonApplication" In this directory I have an other directory : "classes" In "classes" I created 4 python files.

I just want to import one file (and next all the directory "classes") but nothing works! I try this: (in the main file : PythonApplication1.py)

import os
import os.path
import sys
sys.path.append('/classes/classeCarte.py')

print(os.getcwd())
# it gives me : :H/..../PythonApplication1 : its OK

#xxx = os.path.basename(path)
# I tried this : it gives me : "classeCarte.py"
#print(xxx)

carteTEST = Cartes(11, 4)
carteTEST.afficherValeur()
carteTEST.afficherCouleur()

At then end I call functions created in the class: "classeCarte.py" and it doesn't work. I have the error:

names Cartes is not defined

I tried to write at the beginning:

from classes import *

or just:

import /classes/classeCarte.py

It never worked ...

Thank you for your help!

ps: I work on VisualStudio and can see the tree may be is it help on this?

Bulva
  • 1,208
  • 14
  • 28
LucieDevGirl
  • 177
  • 10
  • Possible duplicate of [How to import .py file from another directory?](https://stackoverflow.com/questions/22955684/how-to-import-py-file-from-another-directory) – m13op22 Feb 25 '19 at 17:04

1 Answers1

0

To create an python package can be imported you need to create __init__.py in the folder. The __init__.py can be a empty file. In your case you have to create __init__.py in your classes folder

Then you may use the following import statement: from classes.classeCarte import *

Tempo810
  • 160
  • 7
  • ok thank you very much it works ! but just an observation : it's strange it works because in my code I have the line {from classes.classeCarte import *} underlined in green like it was wrong... the message is : "Unable to resolve 'classe.classeCarte' Intellisense may be missing for this module" – LucieDevGirl Feb 25 '19 at 18:49
  • I am not sure about this. This could the problem if you manually add ```__init__.py``` and the IDE does not interpret this project structure properly. A proper way is to add a package with your IDE, if your project is a python project it should auto add ```__init__.py``` in the package folder and able to interpret the package structure . – Tempo810 Feb 26 '19 at 08:48