473,386 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

newbie question: how to run a python file if it is in a package

for example:

X.py is in aaa.bbb and it has a line like "import aaa.bbb.Y"
how can I run X.py avoiding it saying such like "ImportError: No
module named aaa.bbb"?

Is all runnable script must be in the default package?

thanks.
Sep 5 '08 #1
2 1227
neoedmund schrieb:
for example:

X.py is in aaa.bbb and it has a line like "import aaa.bbb.Y"
how can I run X.py avoiding it saying such like "ImportError: No
module named aaa.bbb"?

Is all runnable script must be in the default package?
There is no such thing as a "default package"

All imports are resolved over the "sys.path" list of directories (or eggs).

There are various ways to modify this:
- by hand. If you know X.py lives below aaa/bbb, you can get it's
__file__-attribute, and walk the way down two levels. Then add the
resulting path to sys.path. All this has to be done before any
import of aaa.bbb occurs.

- modify the environment variable PYTHONPATH to contain the root of
aaa.bbb before starting the script.

- write a custom .pth-file and place it into your pythno installation.
The pth-file will contain the above mentioned root path.

- use setuptools to create script entry-points, and install your whole
application either fully, or as so-called egg-link. Possibly inside
a virtualenv. This would be my personally preferred method.

- simply copy your wohe aaa.bbb-stuff into site-packages. Make sure you
do that whenever you change something in your code.
Diez
Sep 5 '08 #2
On Sep 5, 8:12*pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
neoedmund schrieb:
for example:
X.py is in aaa.bbb and it has a line like "import aaa.bbb.Y"
how can I run X.py avoiding it saying such like "ImportError: No
module named aaa.bbb"?
Is all runnable script must be in the default package?

There is no such thing as a "default package"

All imports are resolved over the "sys.path" list of directories (or eggs).

There are various ways to modify this:

* - by hand. If you know X.py lives below aaa/bbb, you can get it's
* * __file__-attribute, and walk the way down two levels. Then add the
* * resulting path to sys.path. All this has to be done before any
* * import of aaa.bbb occurs.

* - modify the environment variable PYTHONPATH to contain the root of
* * aaa.bbb before starting the script.

* - write a custom .pth-file and place it into your pythno installation..
* * The pth-file will contain the above mentioned root path.

* - use setuptools to create script entry-points, and install your whole
* * application either fully, or as so-called egg-link. Possibly inside
* * a virtualenv. This would be my personally preferred method.

* - simply copy your wohe aaa.bbb-stuff into site-packages. Make sure you
* * do that whenever you change something in your code.

Diez
thanks Diez, i think your answer is almost perfect.
Sep 5 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Manish Gupta (BBS) | last post by:
Hello invoking something like following - sock.send(100) generates the following error - TypeError: send() argument 1 must be string or read-only buffer, not int
0
by: limodou | last post by:
I'm new to setuptools. One question is: Recently I want to use setuptools for a project. My command line is just like : python setup.py sdist --formats=gztar But I found some deleted...
4
by: Daniel Schüle | last post by:
Hello re gurus, I wrote this pattern trying to get the "name" and the "content" of VHDL package I know that the file is a valid VHDL code, so actually there is no need to perform validation...
0
by: | last post by:
Greetings. In an effort to get python2.4 on my Centos 3.7, I installed the python bootstrap rpm. This installed 2.4 alongside 2.2 and updated yum to 2.4.0. Oddly, it didn't create a symlink...
6
by: kath | last post by:
Hi all, Platform: winxp Version: Python 2.3 I have a task of reading files in a folder and creating an one excel file with sheets, one sheet per file, with sheet named...
2
by: Luca | last post by:
Hi all. I'm trying to do something with python import but isn't working for me. Using python 2,5 I've a program structured like this: * a main module called (for example) "mommy" with an...
0
by: Gabriel Genellina | last post by:
En Fri, 25 Apr 2008 15:03:18 -0300, Luca <lucafbb@gmail.comescribió: The short answer is: don't do that! __init__.py may import any module, but other modules in the package should not import...
0
by: Steven Samuel Cole | last post by:
Hi Stephane, thanks for your reply! :-) I do not get any notification or warning or whatever from dpkg, all output I get when running # sudo dpkg -i python-<package name>_0.0.1-4927-1_all.deb...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.