Re: module import questions and question about pytest and module imports

2014-12-08 Thread Ian Kelly
On Sun, Dec 7, 2014 at 9:50 AM, sam pendleton  wrote:
> Having to put the garage package on the sys.path seems a little off,
> why wouldn't relative imports work?

Relative imports are relative to a package's hierarchy of namespaces, not
relative to the file system. As such, you can't perform a relative import
of a module that isn't in the same top-level package (and besides this, the
import statement that you posted was an absolute import anyway).

All top-level packages have to be on sys.path, or the import system won't
be able to find them.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: module import questions and question about pytest and module imports

2014-12-08 Thread Jean-Michel Pichavant
- Original Message -
> From: "sam pendleton" 
> Having to put the garage package on the sys.path seems a little off,
> why wouldn't relative imports work?
> 
> Do most people somehow put their packages in sys.path when bundling
> their python packages up to be shared with setuptools or other python
> package managers? If so, how?

If it feels more natural to you, you can also update the env variable 
PYTHONPATH and add the path to your dev package.
That way you don't need to add python code.

But if you plan to distribute your package with setuptools, the solution would 
probably be to install your package before testing it (and probably use 
virtualenv).

JM


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be 
privileged. If you are not the intended recipient, please notify the sender 
immediately and do not disclose the contents to any other person, use it for 
any purpose, or store or copy the information in any medium. Thank you.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: module import questions and question about pytest and module imports

2014-12-08 Thread Dave Angel

On 12/07/2014 11:50 AM, sam pendleton wrote:

Thanks for getting back with me!

On Sun, Dec 7, 2014 at 11:26 AM, Dave Angel  wrote:

On 12/05/2014 11:50 PM, sam pendleton wrote:


garage/
  |- __init__.py
  |- cars/
  |- __init__.py
  |- hummer.py
tests/
  |- test_cars.py

at the top of test_cars.py, there is this:
  from garage.cars import hummer

pytest is on this import statement, so i guess it's incorrect.



No idea what that statement is trying to say.


Sorry Dave, I was saying that pytest is hung up there stating it can't
import that module.


Why don't you just try running the module, and post the stacktrace when 
it gets an exception?  I've never used pytest, and don't know why it 
would hang on any particular line.






If you're going to import something, it either has to be on the sys.path, or
in the current directory.  Is garage/ on your sys.path?

You can examine sys.path  by
import sys
print(sys.path)


Having to put the garage package on the sys.path seems a little off,
why wouldn't relative imports work?

Do most people somehow put their packages in sys.path when bundling
their python packages up to be shared with setuptools or other python
package managers? If so, how?



When you get to the point of bulding a distribution, you'll be putting 
your packages in the dist_packages directory, which is on sys.path. 
However, generally your distribution utility will handle those details.


In the meantime, you can set some environment variable to add locally to 
your sys.path.


As for relative import versus other choices, that varies between Python 
2.x and 3.x, and you haven't specified exactly what Python version 
you're running or what OS you're on.


In Python 2.x, relative import was considered to be ambiguous, and 
people figured it had to change.  You might want to read


https://docs.python.org/2.5/whatsnew/pep-328.html


--
--
DaveA
--
https://mail.python.org/mailman/listinfo/python-list


Re: module import questions and question about pytest and module imports

2014-12-08 Thread sam pendleton
Thanks for getting back with me!

On Sun, Dec 7, 2014 at 11:26 AM, Dave Angel  wrote:
> On 12/05/2014 11:50 PM, sam pendleton wrote:
>>
>> garage/
>>  |- __init__.py
>>  |- cars/
>>  |- __init__.py
>>  |- hummer.py
>> tests/
>>  |- test_cars.py
>>
>> at the top of test_cars.py, there is this:
>>  from garage.cars import hummer
>>
>> pytest is on this import statement, so i guess it's incorrect.
>
>
> No idea what that statement is trying to say.

Sorry Dave, I was saying that pytest is hung up there stating it can't
import that module.


> If you're going to import something, it either has to be on the sys.path, or
> in the current directory.  Is garage/ on your sys.path?
>
> You can examine sys.path  by
>import sys
>print(sys.path)

Having to put the garage package on the sys.path seems a little off,
why wouldn't relative imports work?

Do most people somehow put their packages in sys.path when bundling
their python packages up to be shared with setuptools or other python
package managers? If so, how?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: module import questions and question about pytest and module imports

2014-12-07 Thread Dave Angel

On 12/05/2014 11:50 PM, sam pendleton wrote:

garage/
 |- __init__.py
 |- cars/
 |- __init__.py
 |- hummer.py
tests/
 |- test_cars.py

at the top of test_cars.py, there is this:
 from garage.cars import hummer

pytest is on this import statement, so i guess it's incorrect.


No idea what that statement is trying to say.



what should it be?


If you're going to import something, it either has to be on the 
sys.path, or in the current directory.  Is garage/ on your sys.path?


You can examine sys.path  by
   import sys
   print(sys.path)


--
DaveA
--
https://mail.python.org/mailman/listinfo/python-list


module import questions and question about pytest and module imports

2014-12-07 Thread sam pendleton
garage/
|- __init__.py
|- cars/
|- __init__.py
|- hummer.py
tests/
|- test_cars.py

at the top of test_cars.py, there is this:
from garage.cars import hummer

pytest is on this import statement, so i guess it's incorrect.

what should it be?

if i open a python repl within tests/, how can i say import hummer.py?

do i need to do anything to make pytest aware of hummer.py?

thanks for the help!
-- 
https://mail.python.org/mailman/listinfo/python-list