3

I copied a module to the "modules" folder of the application in web2py. When I tried to import the module in any function inside of any of the controllers, I get the following error:

<type 'exceptions.ImportError'> No module named module_name

I get that error irrespective of any module used. But if I copied the same module to the "site-packages" and import it, it works perfectly.

I found out that the "sys.path" doesn't contain the modules folder but contains the "site-packages" folder.

How do I add the modules folder to "sys.path" specific for web2py or are there any other ways to solve this problem?

SUB0DH
  • 5,130
  • 4
  • 29
  • 46
  • I think you should try to set-up your project in a [virtualenv](http://pypi.python.org/pypi/virtualenv/1.7.1.2). You will run in a cleaner and simpler environment, and, that's important, you will reduce your chances of breaking everything by doing a mistake. – Alexis Huet Jul 31 '12 at 12:42
  • 2
    Is there an `__init__.py` file in your /modules folder? If not, add one. – Anthony Jul 31 '12 at 15:04
  • @Alexis Even though **virtualenv** would be a solution, I would still like to know how to do it the web2py way. The "modules" folder of each application in web2py seems to be designed for similar purpose as my problem. So, I think it would be better to do it that way. Still, I am also considering your advice. – SUB0DH Jul 31 '12 at 15:06
  • @Anthony Yes, there is a __init__.py in the modules folder, but still doesn't seem to work because the modules folder is not in sys.path of web2py – SUB0DH Jul 31 '12 at 15:09
  • Does web2py still use the local_import function? Is that what you're using SUB? I have never imported a custom module from anywhere but modules and only using local_import... – Kasapo Jul 31 '12 at 15:10
  • @SUB0DH web2py has a custom importer, so when you do `import mymodule`, it looks in the application's /modules folder first. The folder does not have to be in sys.path. What version of web2py? Is it possible the module itself is making an import and failing -- when that happens, I think the custom importer doesn't return the correct error message and just says the module doesn't exist. – Anthony Jul 31 '12 at 15:19
  • @Kasapo As of version 1.96.1, `local_import()` has been deprecated. So, I am using `import` to import my custom module but it doesn't seem to work. – SUB0DH Jul 31 '12 at 15:33
  • @Anthony I am using web2py version 1.99.7 stable. No, the module is not importing anything. I want to make the application logic into a separate file so I kept it in the modules folder to import it whenever required. – SUB0DH Jul 31 '12 at 15:33
  • Not sure what the problem is, then. You might try posting on the Google Group and including some code or even packing and attaching a minimal application that exhibits the problem. – Anthony Jul 31 '12 at 16:43
  • Did you get anywhere with this? I've the same issue. – dmmd Aug 05 '12 at 04:49

5 Answers5

4

Finally found a solution to my problem.

Seems like in order to import a module in the module directory using import module_name, the name of the application must be in all lower case.

Previously, the name of my application was projectX, but when I changed it to projectx the import module_name worked like it should.

I also found out that explicitly stating the application name while importing the module also works, no matter how the application name is. import applications.projectX.modules.module_name also worked for me when the name of the application was projectX.

SUB0DH
  • 5,130
  • 4
  • 29
  • 46
1

Have you already tried to use local import?

Something like this:

twitter = local_import('twitter')

where twitter is the name of module I save in the modules folder.

Cheers

0

It should work exactly as you expect it to, and it does for me. There is no need to change sys.path. web2py looks after this. It expects to find modules in that directory. What version of web2py? You are running from source? (it downloading the source distribution and starting it as python web2py, or are you using a precompiled binary distribution?) When you say "import that module in any function inside a controller", what exactly are you trying to do? For me, a simple import at the top of my default controller, or in a model, works with the current stable version.

Tim Richardson
  • 6,608
  • 6
  • 44
  • 71
  • I am using web2py version 1.99.7 stable. I am running it from the web2py source under Windows 8 Consumer Preview using Python 2.7.3 by running the _web2py.py_ file. I am importing the module by using import statement at the top of my default controller. I have tried using it inside of the functions of controllers but that also results an error. – SUB0DH Aug 01 '12 at 11:31
0

This is the import format that allows PyCharm to resolve the reference for me:

import applications.dashboard.modules.user_info as user_info
0

The fix that I found for this is to be sure that applications is a package.

As an example, if you have the foo application and your tree looks something like this:

applications
├── __init__.py      <----- This file is probably missing
├── yourapplication
│   └── modules
│       ├── ...
├── ...