16

I have following arrangement of files:

python
|---- main.py
|---- files
      |---- folder1
            |---- a.py, a1.py, ...
      |---- folder2
            |---- b.py, b1.py, ...

I wanted to import my modules a.py and b.py to main.py. For this I used the following commands in main.py:

a = 'C:/python/files/folder1'
sys.path.insert(0, a)
from files.folder1 import *

However, I am unable to import modules from folder1 (similarly for folder2).

I get an error:

No module named files.folder1

I cannot use import command as there are many Python files in folder1, folder2, ...

What am I missing here?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
user741592
  • 875
  • 3
  • 10
  • 25

5 Answers5

32

Add a file __init__.py (can be blank) to folders files, folder1 and folder2. Then you got a package files with sub-packages folder1 and folder2. After this you can import from the main.py like this:

from files.folder1 import *
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Yunzhi Ma
  • 662
  • 6
  • 9
19

When I do this in Python 2.7 I use:

import sys
sys.path.append('C:/python/files/folder1')

import a
import a1

Here's a hack I built to import all modules in a directory into a dictionary:

import os
import sys

dir_of_interest = 'C:/python/files/folder1'
modules = {}

sys.path.append(dir_of_interest)
for module in os.listdir(dir_of_interest):
    if '.py' in module and '.pyc' not in module:
        current = module.replace('.py', '')
        modules[current] = __import__(current)

I just built it and it's extremely hacky but it might be more like something you want. So, to access the module you want, instead of saying module_name.thing you would say modules["module_name"].thing

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
mjgpy3
  • 8,597
  • 5
  • 30
  • 51
  • I agree, however, isn't it cumbersome to write many import statements if you have large number of files? – user741592 Nov 28 '12 at 06:22
  • @user741592: Why are you splitting your code up between many files? You should group relevant stuff together into a module. – BrenBarn Nov 28 '12 at 06:23
  • Unfortunately, there is a requirement to split up the code. I would have been happy to group the chunk into a single file. – user741592 Nov 28 '12 at 06:24
  • See the above edit/hack... It might be something more like what you're looking for. – mjgpy3 Nov 28 '12 at 06:36
3

I cannot use import command as there are many Python files in folder1, folder2, ...

What am I missing here?

I believe the part you are missing is the __init__.py file in each of the folders. That file should include an __all__ variable that lists all the submodules that will imported by: from somepackage.subpackage import *.

This is all elegantly explained in the Python Tutorial section on Packages.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
2

If you add folder1 to the path, that doesn't mean you can import folder1 as a module. It means you can import the files inside folder1. So you could do:

import a
import a1

If you want to have folder1 be a package of which a and a1 are modules, you need to put an __init__.py in folder1 and then do import folder1. If you further want to be able to do from folder1 import * and have that import a and a1, you need to put code in your __init__.py that imports a and a1.

If you have a lot of files in a folder that you want to be able to import in a structured way, you should make that folder into a package.

BrenBarn
  • 242,874
  • 37
  • 412
  • 384
0

Looking at the folder structure , what you could do is , Simply

from files.folder1 import a , a1
from files.folder2 import b , b1

and this should help

.

Extras : I recently thought of a problem , of importing pip packages without actually installing them , then what i did was ... i pip installed the package in a virtual environment and copied only folder of the package name

eg. if the package name is qwikidata , then i went to /Lib folder and copied only the qwikidata folder and pasted it in the root of my project , in your case , where the main.py file is and pip uninstalled the package , to avoid the errors

And now i was able to import the package without actually pip installing it since the package was in my root folder of the project

If the project is not so complicated , then it should be fine , but I wouldn't recommend this way of installing packages .

It helps when you want to run your project on some other machine and You dont have an Internet connection .

Mohammad Farseen
  • 75
  • 1
  • 2
  • 8