I've got a strange issue with importing a module. I've got a virtualenv setup and one module is available in {env}/lib/python2.6/site-packages/pkgname
. There is a __init__.py
file and pkgname.py
inside.
Now, if I run {env}/bin/python
and execute import pkgname.pkgname
, it works just fine. But if I create a script in {env}/bin/pkgname.py
with contents:
#!{env}/bin/python
import pkgname.pkgname
if __name__ == "__main__":
pkgname.pkgname.run()
this fails trying to import the same file again (since the package and the file have the same name). How can I disable looking in the same directory? Or ho开发者_JS百科w can I force the import to first look at the global packages?
Alternatively, what's the "proper" way of doing this? Just for consistency, I'd rather call my startup script the same as the actual package it's trying to run.
Call it pkgname. Done. OK, then it won't start if you doubleclick in it WIndows, but that's usually not a problem.
You can modify sys.path. It's just a list of paths to search and the current folder should be the first entry. Your file should run if you move the current folder to the end of the list. But I general I would not do something like that without a VERY good reason. Isn't it possible to rename your file name.py, runpkgname.py or something like that?
精彩评论