SUB0DH
SUB0DH

Reputation: 5240

web2py doesn't recognize modules in the modules directory

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?

Upvotes: 3

Views: 5064

Answers (5)

halfdime
halfdime

Reputation: 1

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
│       ├── ...
├── ...

Upvotes: 0

Ben Wolski
Ben Wolski

Reputation: 3

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

import applications.dashboard.modules.user_info as user_info

Upvotes: 0

SUB0DH
SUB0DH

Reputation: 5240

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.

Upvotes: 4

Walter Traspadini
Walter Traspadini

Reputation: 869

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

Upvotes: 1

Tim Richardson
Tim Richardson

Reputation: 7231

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.

Upvotes: 0

Related Questions