last day (15 days later) » 

5:50 PM
0
A: Importing module item of subpackage from another subpackage

idjawI think because of where you are running your code from, your modules need to be imported with respect to the root of execution. Therefore, I think if you change this: from root_package.packB.moduleB import to this, it might work: from packB.moduleB import EDIT from OP update So, for the Py...

 
Although this worked, it created some other problems. Also, it seems PyCharm doesn't like the change (I've included a screen shot at my OP).
I've added another update to my OP, could you please take a look?
 
So, to focus on the command line. For this from root_package.rootModule import RootModuleClass. If you are running your code within 'root_package', you cannot set your module import as such. The root of execution does not know of root_package at all.
There just seems to be a bit of a mess going on between what you are configuring to run in Pycharm and your expectation of where you are running in the command-prompt. It would be so much easier if I were sitting next to you :P . We will get this working ;)
 
I am running the test_rootModule script from the project_folder (the root_package is a subfolder of project_folder), that's why I've included the from root_package.rootModule import RootModuleClass. That is true :P Thank you for your help so far idjaw! Hope we can figure this out
 
You're very weclome. I've been through these very problems...I get it. So, just to get things clear. The way things are configured in PyCharm, everything works perfectly fine right? You are now strictly facing problems with the command-line
Hey
This makes more sense
 
Hello from here idjaw,

Yes, both `rootModule` and `test_rootModule` work just fine via PyCharm.
Also, running `rootModule` via terminal works fine, and this is how I run it:
`python ./rootModule.py` when I am in `project_folder/root_package/` directory.
(Also, runs just fine if I am at `project_Folder` directory and running it like this `python ./root_package/rootModule.py`)
 
5:54 PM
so now the issue is when you are running your tests from commandline and because your project is structured like this right... [project_root/tests] [project_root/root_package]
 
Hmm, the markdown doesn't seem to work in the chat site.

Well, the project is structured as such:
[project_folder]
[project_folder/root_package]
[project_folder/test_rootModule.py]
 
yes, that is what I meant...perfect. OK. So I understand the structure of the code. So, just out of curiosity, how are you running your test_rootModule.py in Pycharm
because when I set up my tests, I set up a test folder, and then I configure nosetests for that test folder, and structure my application properly so that my references are all referenced the same way, to not remove confusion about where they are executed from.
Also...because I am depending on nosetests, it makes it easier to manage when I move it to another system to test. For example, I upload all my apps to Travis, and make sure it runs within 'tox'. To properly encapsulate my environment
I don't meant to go on a bit of a tangent, but maybe we can look at this a bit differently to get this working
 
Well, I've created a run configuration with the WORKING_DIRECTORY being set to "/project_folder/" and with the first 2 checkboxes checked (Add contents/source roots to PYTHONPATH).
Hmm, I haven't use these applications yet. Are these for a web application or any python application?
If you'd like a screenshot of the run configuration, I can easily post one to imgur - whatever you need.
 
I was about to do the same with mine actually. So, feel free to share whatever you feel like. I was going to do the same. Actually...maybe this might help you, but let me show you how my configuration looks
let me know when you've taken a look
So, the main point I have been trying to make when looking at my nosetest configuration
you can see what I did for my working directory
That is where all my code is referenced from
so all my imports are with respect to that working directory
This is akin to chdir...like this post: stackoverflow.com/questions/1432924/…
 
6:15 PM
I've seen the screenshot and I get that you change the working directory to the root of the references of all the imports in your project (right?).
I was trying to see how to apply this in my project and the problem is that, I can already run the rootModule via terminal from either the [project_folder] directory or the [root_package] directory. And the test_rootModule is at the [project_folder] directory.
Let me post some screenshots
Firstly, I should prepare you with the aliases I've created for the StackOverflow post and their equivalents
1. project_folder == lossy-compression
2. root_package == lossy_compression
3. packA == algorithms
4. moduleA == wavelet_compression
5. packB == util
6. moduleB == files_util.py
 
awesome. ok
 
Here is the album of the screenshots in this order:
1. project structure
2. terminal output (firstly running lossy_compression.py and then test_lossy_compression.py)
3. run configuration of test_lossy_compression.py that is working via PyCharm
 
OK. Something seems a bit off....Your modules in your stack trace start with lossy_compression except for utils. Have you tried adding lossy_compression.utils instead?
Also, just to note...that if you are running within lossy_compression, your modules shouldn't start with lossy_compression
it is working...but something doesn't seem right with those references
 
Hmm, this also "from algorithms.wavelet_compression import WaveletCompression" doesn't start with lossy_compression, right? (or am I looking somewhere else?)
Also, I am running within lossy-compression - not lossy_compression (this is a bad naming choice, sorry for that)
 
6:30 PM
No need to apologize...but you mentioning that made me realize something. You have lossy-compression/lossy_compression/*code*
hmm..hold up
ok...let's restart a second
I think I might have it
(maybe :P)
So, if we move in to /home/chris/development/lossy-compression
now...answer this...is that where you want to run everything from?
if yes....then all your references need to start with from lossy_compression.blah.blah
because in lossy-compression you have a folder called lossy_compression right?
 
Exactly! ;)
This is why in the first place I had all my imports as lossy_compression.sth
 
:) sorry man...I completely missed the lossy-compression/lossy_compression
I'm realizing now the directory references now
So..now that I've seen the light....what is the latest with the problem?
 
It's no problem, it was a bad naming convention :/
You mean, what is the current problem?
 
yeah
are we back at square one/
 
6:47 PM
Hmm possibly :P
 
well....crap
 
If the root_package of my application is lossy_compression, should I prefix all the imports with "lossy_compression."
?
 
yes
absolutely
if you are running from lossy-compression
and the path to all your code is inside lossy_compression
then absolutely, prefix all with lossy_compression
I think that traceback error you had with 'utils'
is exactly that....you can start with that
because your mother modules already had lossy_compression in front
 
Ok, so as I had it at start, let me fix these imports and report back ;)
Once, again thanks a lot for your effort, I hope I am not wasting your time.
 
not at all....It's the end of my sprint at work, so I'm actually just browsing stackoverflow for fun. :) I have to leave in 10 minutes for a personal appointment. So I'm all yours for 10 minutes.
 
6:52 PM
Ok great!
So now, I've prefixed all the imports at packages util and algorithms with "lossy_compression."
 
alright
 
At the lossy_compression.py module these are the imports:
from algorithms.wavelet_compression import WaveletCompression
from algorithms.avg_compression import AveragesCompression
from algorithms.adaptive_avg_compression import AdaptiveAVGCompression
from algorithms.linear_regression_fitting_compression import LinearRegressionFittingCompression
from util.csv_reader import CSVReader
from global_constants import WAVELET_ALGORITHM, AVG_ALGORITHM, AD_AVG_ALGORITHM, \
LIN_REGR_FITTING_ALGORITHM, PROGRAM_NAME
 
hmm
I'll have to answer with an example to make sure I answer correctly
I'm referencing one of my projects
actually
hold on
so
 
Hmm, so no need to prefix the imports at the lossy_compression.py module
 
if you look at the top of my imports, I have exceptions.py which is in the same location as restore.py
I am importing it as such from core.exceptions import RestoreError
because the root of my application is 'app'
so my module references will be everything inside app
hence core.stuff.foo
 
7:01 PM
So this means, that I shouldn't prefix all the imports with lossy_compression
Since, you are not prefixing them with 'app' ;)
right?
 
well I am running my application inside app
so where my module is core
you can see that as lossy_compression
so I think you actually have to prefix it with lossy_compression
back to my code....restore.py and exceptions.py both sit inside my core module
inside restore.py I still have to reference it as core.exceptions
because my application root is inside app
so my references have to start with core.*
for you that is lossy_compression.*
 
Yes, they do sit there, but the root package is 'app', and my root package is 'lossy_compression'.
When adding 'lossy_compression' as a prefix, I get the same error as the one we started :/
"ImportError: cannot import name WaveletCompression"
 
ok
 
I think following your application, I should prefix with my root package.
 
sorry I thought you were running above that level....apologies
 
7:05 PM
Hmm, I'd like your opinion on where this script should be run from
 
If I were to make this as abstract as possible, and take how I run my apps
this is what I do
project_name/app/
now
inside app
I will set up the different modules I need. Like my helpers modules that i define as utils, all my test framework as tests
then I usually set up a folder called core (or anything else that defines the core logic of your application)
I make sure my code root is inside 'app'
that way I can reference all my code from the same level
this includes my test framework
it makes it easier for me to manage
feel free to use my code as reference for structure
I unfortunately need to head out...I have an appointment with a notary
I can leave this window open and I'll probably be back a little later this evening.....it's a subject I'm pretty interested in :)
create a folder called tests inside lossy-compression
so you can maybe create more tests if you want and it is encapsulated in a "tests" environment
your lossy_compression seems to be the equivalent of my core
anyway...don't want to throw around any confusion....take a look at my structure...see what you think...mess around with some stuff....this is just a case of referential hell
reference hell
good luck...let me know how it goes...I'll check back in a few hours
 
Hmm, I will look into it and post back if I have any update on the outcome :)
Yes, I will try creating a tests-package and see how this works!

Once again, thanks a lot for your help! Honestly, you've been most helpful. :)
If the window closes, I think you can re-access the chat via the link in my question ;)
Note: To avoid confusion, I've renamed the project folder "lossy-compression" to "thesis"
So, now we have [thesis/lossy_compression]
 
 
4 hours later…
11:29 PM
Hey! just an FYI...online for the evening.
 

  last day (15 days later) »