Python Forum

Full Version: Error while importing my own package
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everybody,

I am trying to make and call my own package but something is not working. This is how my code is structured:
  • Main Folder1
  • Main Folder2
    • Folder1
      • SubFolder (this is what i want to export as package)
        • SubSubFolder1 (contains __ini__.py)
        • SubSubFolder2 (contains __ini__.py)
        • SubSubFolder3 (contains __ini__.py)
      • __init__.py
    • Folder2 (i need to use my package here)

As i read online i need to add an __init__.py file to each file i want to be in the package.
In folder2, i am calling the package in the following way:

from subFolder.SubSubFolder1 import file.py
However it says:
Error:
Module not found error:No module named 'subFolder'


Thanks
You should add Folder1/ to your PYTHONPATH:
export PYTHONPATH=$PYTHONPATH:/home/username/Main Folder2/Folder1
Or even all the content of Main Folder2/...
export PYTHONPATH=$PYTHONPATH:/home/username/Main Folder2
And import like..
from Folder1.subFolder.SubSubFolder1 import file.py
(Sep-18-2018, 02:24 PM)gontajones Wrote: [ -> ]You should add Folder1/ to your PYTHONPATH:
export PYTHONPATH=$PYTHONPATH:/home/username/Main Folder2/Folder1
Or even all the content of Main Folder2/...
export PYTHONPATH=$PYTHONPATH:/home/username/Main Folder2
And import like..
from Folder1.subFolder.SubSubFolder1 import file.py

Hello,
Thanks for helping but that didn't work. I couldn't find a PYTHONPATH in the environmental variables so i created one adn added the folder but this didn't help.
The "space" in "Main Folder2" is the problem.
It is not a good practice use space to name folders/files.
You can rename it to something like "Main_Folder2"

export PYTHONPATH=$PYTHONPATH:'/home/username/Main_Folder2'
Ok that worked. I maanually added PYTHONPATH in the environmental variables and it's working. Then i had to change a few
"imports" in my code (needed to add the right folder).

Thanks