Python Forum
Error while importing my own package
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error while importing my own package
#1
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
Reply
#2
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
Reply
#3
(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.
Reply
#4
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'
Reply
#5
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  qpython package error Creepy 4 1,007 Jul-06-2023, 05:58 PM
Last Post: Creepy
  pyarrow error when importing pandas sravva 1 933 Jun-06-2023, 05:09 PM
Last Post: snippsat
  ERROR: importing desired module mbgamer28 0 1,674 Apr-05-2021, 07:46 PM
Last Post: mbgamer28
  countryinfo package error MarcusAurelius 4 2,821 Mar-20-2021, 05:42 PM
Last Post: deanhystad
Bug Error while importing numpy Erfan 3 3,268 Nov-28-2020, 07:49 AM
Last Post: bowlofred
  error with netcdf4 package damdam 0 1,825 Nov-18-2020, 06:44 PM
Last Post: damdam
  Importing module from a package results in import error goghvv 2 2,382 Mar-27-2020, 07:13 PM
Last Post: goghvv
  Beginner - simple package installation error mefeng2008 0 1,717 Mar-13-2020, 09:17 AM
Last Post: mefeng2008
  Error importing package julio2000 3 3,756 Jan-26-2020, 06:15 PM
Last Post: buran
  Error in importing numpy fullstop 1 1,915 Dec-18-2019, 12:07 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020