I have the following folders structure:
myappdir
- libs
- somelib
script1.py
script2.py
- google
- protobuf
__init__.py
message.py
...
__init__.py
...
app.yaml
appengine_config.py
...
And the following files content -
appengine_config.py:
import sys
sys.path.append('libs')
script1.py:
from somelib.script2 import Something
script2.py:
from google.protobuf import message
In result I get:
File "myappdir/libs/somelib/script1.py", line 34, in <module>
from somelib.script2 import Something
File "myappdir/libs/somelib/script2.py", line 38, in <module>
from google.protobuf import message
ImportError: No module named protobuf
What is wrong with my setup?
Change the lines in your appengine_config.py file, from:
import sys
sys.path.append('libs')
to:
import sys
import os.path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'libs'))
I found #Daniel's answer to already be implemented in my setup, but still had the problem. This github comment helped me. Adding the following to appengine_config.py solved the problem for me:
from google.appengine.ext import vendor
vendor.add('lib')
import google.protobuf; print(google.protobuf.__version__)
change lib to libs depending on your project directory naming.
Related
So my project structure is the following:
project/
src/
__init__.py
utils.py
model.py
usage.py
I now want to import functions from utils.py and a class in model.py into my usage.py. But the model.py itself imports functions from utils.py.
So I am doing the following:
# usage.py
from src.model import Model
from src.utils import onehot_to_string
But I am getting the error that it couldnt import functions from utils.py into the model.py:
File "usage.py", line 11, in <module>
from src.model import *
File "/project/src/model.py", line 7, in <module>
from utils import onehot_to_string
ImportError: cannot import name 'onehot_to_string' from 'utils' (/lib/python3.7/site-packages/utils/__init__.py)
I think I am lacking of some basic Python packaging knowledge here. Can someone help me out? :)
Looks like python can't find your utils file in model.py. Then it proceeds to search for utils in path and finds it because, for example, someone has installed some library named utils. Then, the error occurs because this previously installed utils library has no onehot_to_string function.
Try to change your from utils import onehot_to_string to from .utils import onehot_to_string in model.py to use relative import.
for file/function/variables importing use this sys method
import sys
# insert at 1, 0 is for other usage
sys.path.insert(1, '/path/to/application/app/folder')
Python 3.6+, directory structure:
project/
main.py
subpackage/
submain.py
config.py
main.py:
from subpackage.submain import attribute1
if __name__ == "__main__":
print(attribute1)
submain.py:
from config import config_param
attribute1 = 1 + config_param
config.py:
config_param = 100
it throws error:
Traceback (most recent call last):
File "/projects/test/project/main.py", line 1, in <module>
from subpackage.subname import attribute1
File "/projects/test/project/subpackage/subname.py", line 1, in <module>
from config import config_param
ModuleNotFoundError: No module named 'config'
We use couple gitsubmodule in our project and for now it works with:
from .config import config_param
But I am sure that this is wrong solution because it is already require in some folder/folder/folder to create such relative import ...config or ../../../config.
I already spent a week to find how to do this so that subpackage could work independently(from config import config_param), any help, links appreciate, thanks
try to add __init__.py files in your dirs https://docs.python.org/3/tutorial/modules.html
One solution that should work :
Add __init__.py in the subpackage,
Add the subpackage directory in PYTHONPATH, you can also do it with :
sys.path.append(PATH_TO_THE_SUBPACKAGE) .
The package is now part of the PYTHONPATH, so you can import it like any other libraries with import config.
I have a directory structure like the following:
/
/setup
/sqlalchemy
__init__.py
metadata_setup.py
/server
/data
__init__.py
simulations.py
In simulations.py I have:
import sys
sys.path.insert(0, '/setup/sqlalchemy')
import metadata_setup
but then I get the following error at the import statement:
ModuleNotFoundError: No module named 'metadata_setup'
I tried adding a __init__.py to the root directory but I'm still getting the same results. I'm running Python 3
Either insert full path or edit relative path like:
sys.path.insert(0, '../../../setup/sqlalchemy')
This worked for me:
import sys
sys.path.insert(0, os.path.join(os.path.dirname(sys.path[0]),'setup', 'sqlalchemy'))
import metadata_setup
I have gone through numerous answers and tried most of them as well but still facing the same issue
My structure
-- backend
-- app
-- __init__.py
-- utils.py
-- models.py
-- pytest
-- test_utils.py`
Now I want to import utils into the test_utils to be able to call the function and test it. My init.py is not empty and my test_utils.py looks like this
import sys, os.path
sys.path.insert(0 ,(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) + '/app/'))
from utils import test_function
def test_test_function():
a = test_function(5)
assert a == 5
I checked my sys path is also pointing to correct directory but keep getting this error and I am using Python 2.7 in Linux
ImportError while importing test module '/home/richie/Documents/Project/backend/pytest/test_utils.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
../virenv/local/lib/python2.7/site-packages/six.py:709: in exec_
exec("""exec _code_ in _globs_, _locs_""")
test_utils.py:3: in <module>
from utils import test_function
../app/utils.py:15: in <module>
from models import Users, Accounts
../app/models.py:2: in <module>
from app import db
E ImportError: No module named app
The solution is to create a pytest.ini file in pytest/ folder.
The content of pytest.ini would be:--
[pytest]
python_paths = . ../app
Turns out that have to add path to both app and utils and then it works
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
sys.path.append((os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) + '/app/'))
I have a folder structure like so:
/mylib/
/mylib/__init__.py
/mylib/my_class.py
/mylib/tests/test_my_lib.py
In my test, I have:
from mylib import MyClass
import unittest
I'm getting:
File "test_edgecast_mcc_client.py", line 1, in <module>
from mylib import MyClass
ImportError: No module named mylib
Which, I think, makes sense because the import would be looking inside the tests directory for mylib when it should be looking in ../mylib?
Can anyone share some light on how to get the import to work properly?
I believe your tests package needs an __init__.py file too
Add primary directory to $PYTHONPATH. You can do it from your test_my_lib.py using something like this:
import sys
import os.path
d = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, d)
try using this from command line and see if that eliminates the error
export PYTHONPATH="$PYTHONPATH:/path/to/mylib/"