I'm trying to package my modules, but I can't seem to get it working.
My directory tree is something like the following:
snappy/
__init__.py
main/
__init__.py
main.py
config.py
...
...
and the code I'm using is
from snappy.main.config import *
I'm getting the error:
ImportError: No module named snappy.main.config
Any ideas what's going wrong? This is using Python 2.5 on Ubuntu 8.10.
Thanks in advance for your help.
It depends on where your script using the import resides and your system PYTHONPATH. Basically, to have that import working you should run your script (the one having the import) in the parent directory of snappy or your script should change sys.path to include it.
./alex
Is the parent directory of snappy in sys.path? If it's not, that's the only thing I can think of that would be causing your error.
Related
I'm trying to import a class in a different directory to another file, but can't seem to get it to work. I know this question has been asked a lot and I have looked through multiple stackoverflow solutions and at https://docs.python.org/3/tutorial/modules.html#packages
1: Importing files from different folder
2: import python file in another directory failed
I want to try to just use the method containing just __init__.py file instead of doing an import sys
My directory structure is as follows:
django_vue/
__init__.py
devices/
__init__.py
models.py
lib/
__init__.py
my_file.py
I'm trying to import the class Device from /django_vue/devices/models.py to /django_vue/lib/my_file.py by:
from devices.models import Device
However when I do that I still get the error:
from devices.models import Device
ModuleNotFoundError: No module named 'devices'
I'm not sure what I'm dong wrong since I already have the __init__ file in both directories. Any help is appreciated. Also I'm running python 3.6.
This is the folder structure I'm working with.
.
└── django_vue
├── devices
│ └── models.py
└── lib
└── file.py
When you run
$ python file.py
python has no way of knowing what's outside the directory.
python can't go back and then into devices/ just like that.
The easiest way to solve this would be to add the folder devices/ to sys.path. When python imports a module, it searches for the module from sys.path. Adding the path to devices/ would make it available for imports.
Here are my files.
# models.py
Device = 'device'
# file.py
import sys
sys.path.append('..') # adds the parent dir (which is django-vue/) to path
# django-vue dir has devices/ so now this is available for imports
# importing this works now
from devices.models import Device
print(Device)
Output
django_vue/lib$ python3 file.py
device
Think about it your are inside my_file.py and import something called devices.
How can python know where the name devices has come from.
It won't search your entire Drive for that module/package
Relative Import
use a relative import instead. write from ..devices.models import Device. This is telling python to go up one directory to the parent directory and that's where it will find the devices package. Your lib module should now work as a module
If you however run the my_file.py package directly (as in python C:/django_vue/lib/my_file.py)
You will still get an error. Not the same error; the new error will be something like
ImportError: attempted relative import with no known parent package
This is happening because you are actually running my_file.py
If you think about it why would you want to run my_file.py by itself when it is clearly a part of a package. Maybe you are just testing to see if the import works when you use your package. The problem with this is that it makes it seem like your packages relative imports don't work even though this actually works.
Create a main.py in django_vue and write from lib import my_file. This will run your my_file.py and you will notice there is no error.
What's happening here
Have you heard of __package__?
if you put print(str(__package__)) in your my_file.py and run my_file.py directly you will see that it prints None.
However if you run main.py (that you just created) you will see that when It reaches my_file.py, __package__ will actually be defined to something.
Ahhh... you see now it all makes sense; The error you originally got said something about no known parent package. If __package__ is undefined that means there is no relative parent package because the file was obviously run directly instead of as part of a package.
Consider Absolute imports
you also might want to consider using absolute imports because if you are working on the package you might change it directory structure while developing. so you need to keep changing the import references on the affected files.
Although you can find IDE's with python extensions that automatically to this as you change your directory. I believe VS Code does this automatically.
Replace the __init__ files with __main__.
The structure is:
hello/
__init__.py
params.py
bye/
__init__.py
params2.py
I want to call constant A that lives in params from params2 file...
I tried:
from ..hello.params import A
But I get the following error:
ValueError: Attempted relative import in non-package
hello is not a package?
Thanks in advance!!!
Your code is not working because you are trying to import something that is one level up relative to the directory hello
To make it work you need to be aware what is your PYTHONPATH. If there is hello/bye in PYTHONPATH, this may work:
from ..params import A
It might be also the case that there is your project root hello in PYTHONPATH, then you may just do
from params import A
All depends on how you have installed your package or which IDE config you are currently using or which path you have explicitely added to PYTHONPATH
Before you mark this as a duplicate please read the whole problem, it is not the usual sys.path import error.
This is the directory structure of the module I'm trying to import
root
|_moduleDir1
| |_ myModule1.py
| |_ __init__.py
|_ moduleDir2
|_ myModule2.py
|_ __init__.py
I am working with 2 different machines. My script runs fine on one and I get the import error on the other. At first I thought it was just an environment issue, but I think something else is going on. When I run my script which imports myModule1.py with
import moduleDir1.myModule1
on the failing machine it throws the following import error.
ImportError: No module named myModule1
The reason I don't believe it is a PATH issue or a sys.path issue is that I have added the root directory to the appropriate environment variables and when I add
import moduleDir2.myModule2
above the myModule1 import in the script, it doesn't throw an import error so I know root is in sys.path or it would throw an error for them both. Also I don't think it is some issue with init.py because it works fine on the other machine. Both machines are running windows 10.
I'm stumped and I have searched and tested for hours with no luck. All I can find is posts that are issues with init.py or sys.path and those are not the right solutions for my problem. Any suggestions or thoughts on why else I might be getting an import error for one module and not the other when they are under the same parent directory would be appreciated.
One of reason may be the second machine that throws an import error does not have the read permission of directory.Please check the permission of the directory.
Let's say my project structure looks like this:
app/
main.py
modules/
__init__.py
validation.py
configuration.py
modules package contains reusable code.
main.py executes main application logic.
When I try this in main.py
from modules import validation
I get an error which says that import inside of the validation failed. Validation tries to import configuration and I get 'no module named configuration'
I am using Anaconda distribution on windows.
What is the best way of handling PYTHONPATH during development of the package ?
Is there a way to utilize virtualenv (or conda env) in order to get package, that is in development,on the PYTHONPATH without changing sys.path from the code ?
What is the preferred practice when developing a package ?
I've also tried adding modules (folder) package to the lib/site-packages but it still didn't work.
Change your import in validation.py to:
from . import configuration
This is needed for Python 3 but also works with Python 2.
My projects are generally structured like this:
projectname/
__init__.py
python/
mymodule.py
other_stuff/
more_stuff/
where __init__.py contains the following code
import os
mypath = os.path.dirname(os.path.realpath(os.path.abspath(__file__)))
__path__ = [mypath, mypath+"/python"]
This "skips" the python directory when importing to allow python code in the form from projectname import mymodule rather than from projectname.python import mymodule.
This appears to break pylint however, being unable to import any modules in the project despite $PYTHONPATH being set correctly. Creating a softlink projectname -> python in the projectname fixes things but isn't a suitable solution.
Any suggestions on how to fix this without altering the directory structure?
I think you're kind of stuck. Pylint doesn't process your __init__.py file so unless you can find another way of getting that information into pylint, I don't think it's going to work. Good luck.