Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relative importing python module from a subfolder from a different subfolder

I'm trying to use alembic which is an sqlalchemy tool in python. You type a command and it generates a folder "alembic" with py files inside. The py file inside, needs to link to my application in a separate folder called "myapp". But I cannot link it. It says it doesn't exist and relative import doesn't work.

so I need to import my config class from myapp/configs/config.py file.

/apps
+--/alembic
|----env.py <--- the calling file
+--/myapp
|----configs/__init__.py <--- has "DefaultConfig" class imported
|----configs/config.py <--- I want to import the class inside here.

inside env.py:

from myapp.configs import DefaultConfig

Doesn't work.

I tried:

from ..myapp.configs import DefaultConfig

No success.

example code in alembic docs say just use "myapp.whatever".

I even added my "/apps" and "/myapp" to PYTHON_PATH in environment variables.

Example error:

File "D:\apps\myapp\lib\site-packages\alembic\command.p
y", line 97, in revision
    script.run_env()
  File "D:\apps\myapp\lib\site-packages\alembic\script.py
", line 191, in run_env
    util.load_python_file(self.dir, 'env.py')
  File "D:\apps\myapp\lib\site-packages\alembic\util.py",
 line 186, in load_python_file
    module = imp.load_source(module_id, path, open(path, 'rb'))
  File "alembic\env.py", line 5, in <module>
    from ..myapp.configs import DefaultConfig as conf
ValueError: Attempted relative import in non-package
like image 983
Dexter Avatar asked Feb 04 '13 07:02

Dexter


People also ask

Can import module from another folder Python?

The most Pythonic way to import a module from another folder is to place an empty file named __init__.py into that folder and use the relative path with the dot notation. For example, a module in the parent folder would be imported with from .. import module .

How do you use relative import in Python?

Relative imports use dot(.) notation to specify a location. A single dot specifies that the module is in the current directory, two dots indicate that the module is in its parent directory of the current location and three dots indicate that it is in the grandparent directory and so on.

Does Python search subdirectories for import?

The PYTHONPATH is the environment variable that contains the path of the directories that Python searches to import the packages. Therefore, if we add the subdirectory to the PYTHONPATH , Python will first look at the directories in PYTHONPATH and import it from there.


1 Answers

You have two possible solutions to your problem:

Modify your PYTHONPATH environment variable

Add the path to apps catalogue by running following BASH / SH shell commands in your terminal:

$ export PYTHONPATH=$PYTHONPATH:'/path/to/apps'

Please not that adding it to the PATH environment variable won't work. To find out more about PYTHONPATH, how to manage it plus nice and friendly info on modules in general:

http://www.stereoplex.com/blog/understanding-imports-and-pythonpath

Please note however that this approach does affect your system's PYTHONPATH. It is highly recommended to use a virtualenv - just in case things go wrong, it won't affect all your system and other apps. When using virtualenvwrapper:

$ add2virtualenv '/path/to/apps'

More HERE.

Append path from inside the Python script

Alternatively, you can do the same but just for a script runtime by adding:

import sys
sys.path.append('/path/to/apps')

to your apps/alembic/env.py file.

Finally, in same file, make a following change:

from myapp.configs.config import DefaultConfig

And please note that your apps/myapp folder should also contain __init__.py file (might be empty) to make Python to treat is as a module as Demian Brecht pointed out.

like image 146
Kulbi Avatar answered Nov 15 '22 16:11

Kulbi