有什么办法不创建.pyc文件? [英] Any way to not create .pyc files?

查看:65
本文介绍了有什么办法不创建.pyc文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之:


有没有办法运行Python而不试图创建.pyc文件(或

..pyo)或拥有Python不要尝试导入它找到的.pyc文件?


原因:


我们在网络驱动器上安装了特定于站点的软件包[ 1]。当

任何有写访问权限的人导入此软件包时,网络驱动器都会收到带有.pyc文件的垃圾邮件。


如果这些。 pyc文件存在,当其他用户的Python解释器使用它们而不是.py文件时,它们似乎会引起问题。 (我知道,

他们*应该*工作,但他们不是)。这可能有一些事情可以做。

事实上所有这些用户(在Windows上)都有网络

驱动器映射到任意驱动器号。我不知道。


PEP 304会有所帮助,但似乎已经死了。我想要的是一个命令行选项


我将不得不拼凑一个解决方案,比如

导入的模块在导入后立即删除自己的.pyc文件,

但是如果有一个/好/不使用.pyc的方式真的很好......
< br $> b $ b(脚注)


[1]因为分发代码更新是一个行政噩梦

到几十个非技术用户

In short:

Is there any way to run Python WITHOUT trying to create .pyc files (or
..pyo) or to have Python not attempt to import the .pyc files it finds?

Reason:

We have a site-specific package installed on a network drive[1]. When
anyone with write access imports this package, the network drive gets
spammed with .pyc files.

If these .pyc files exist, they appear to cause problems when other
users'' Python interpreters use them instead of the .py files. (I know,
they *should* work, but they don''t). This may have something to do
with the fact that all of these users (on Windows) have the network
drive mapped to arbitrary drive letters. I don''t know.

PEP 304 would have helped, but it appears to be deceased. What I
really want is a command line option

I''m going to have to cobble together a work-around, like having
imported modules delete their own .pyc files immediately after import,
but it would be really nice to have a /good/ way of not using .pyc...

(footnotes)

[1] Because it''s an administrative nightmare to distribute code updates
to dozens of non-technical users

推荐答案

Lonnie Princehouse写道:
Lonnie Princehouse wrote:
有没有办法运行Python而不试图创建.pyc文件(或
.pyo)或者Python没有尝试导入它找到的.pyc文件?
Is there any way to run Python WITHOUT trying to create .pyc files (or
.pyo) or to have Python not attempt to import the .pyc files it finds?




你可以将你的软件包卷成一个zip存档然后导入它。对于

实例,将你的main.py从存档中取出并放入其他所有内容

in。然后,在main.py的顶部:


import sys

sys.path.insert(" network_path / package.zip")

导入包

#在这里使用包来做普通的东西。


只要你压缩包,就会删除所有.pyc和.pyo文件

,Python我们别无选择,只能每次编译文件

它们都被导入 - 除非我非常误以为Python不会把pyc

文件放到zip中存档,或修改已存在的任何内容。


至于将更新后的副本分发到各个工作站的维护问题,IMO这只需要一点点事先考虑并且

然后它根本不是头疼的事。而不是用户直接启动

应用程序,他们可以启动一个启动应用程序,用服务器检查

,以确定是否需要更新本地文件,如果

所以抓住它们然后启动主应用程序。这实际上消除了Windows世界中令人头疼的
头痛,你无法直接更新当前正在运行的
程序。


我过去所做的事情增加了入门应用程序的想法,并且

有主应用程序检查是否有更新

启动器应用程序,如果是这样,那么在退出

主应用程序时将这些更改拉下来。我刚刚在INI文件中保存了文件位置。


-

Paul McNett
http://paulmcnett.com



You could roll your package into a zip archive and then import that. For
instance, keep your main.py out of the archive and put everything else
in. Then, at the top of main.py:

import sys
sys.path.insert("network_path/package.zip")

import package
# do normal stuff with package here.

As long as you zipped up the package will all .pyc and .pyo files
removed, Python will have no choice but to compile the files every time
they are imported - unless I''m grossly mistaken Python won''t put the pyc
files into the zip archive, or modify any that were there already.

As far as the maintenance headache of distributing updated copies to
individual workstations, IMO this just requires a little forethought and
then it isn''t a headache at all. Instead of the users starting the
application directly, they could start a starter application that checks
with the server to determine if local files need to be updated, and if
so grab them and then start the main app. This actually removes
headaches in the Windows world, where you can''t drop-in updates to
programs that are currently running.

What I''ve done in the past adds on to the starter application idea, and
has the main application check to see if there are updates to the
starter application, and if so pull those changes down upon exit of the
main application. I just saved the file locations locally in an INI file.

--
Paul McNett
http://paulmcnett.com


Lonnie Princehouse写道:
Lonnie Princehouse wrote:
简而言之:

有没有办法运行Python而不试图创建.pyc文件(或
.pyo)或让Python不尝试导入。它找到的pyc文件?

原因:

我们在网络驱动器上安装了特定于站点的软件包[1]。当具有写入权限的任何人导入此包时,网络驱动器会被.pyc文件发送垃圾邮件。

嗯,至少他们有权创建它们。但是,这不是很简单,只需要创建所有的.pyc文件。

如果存在这些.pyc文件,它们似乎会导致其他问题<用户的Python解释器使用它们而不是.py文件。 (我知道,
他们*应该*工作,但他们不是)。这可能有一些事情要做,因为所有这些用户(在Windows上)都将网络
驱动器映射到任意驱动器号。我不知道。


.pyc文件通常在与.py文件相同的目录中创建。

您可能会遇到问题,因为一个用户没有权限

访问另一个创建的文件。在这种情况下,您可以设置

继承的创建者所有者权限,以允许所有用户阅读。


如果有一个用户,您可能也会遇到问题正在使用python 2.2和

另一个使用2.4,因为每个版本需要自己的格式为.byc文件,并且它们可能会发生冲突。 Ulitmately听起来像是一个

权限问题。

PEP 304会有所帮助,但它似乎已经死了。我真正想要的是一个命令行选项

不确定它已经死了(死鹦鹉?) - 它在标准轨道上,

它没有被拒绝,而Skip实际上提供了一个补丁来实现解决方案。

我将不得不拼凑一份工作 - 就像导入的模块导入后立即删除自己的.pyc文件一样,
但是如果有一个/好/不使用.pyc的方式真的很好......

找到

问题的根本原因并实际修复它们会更加明智:-)

(脚注)

[1]因为向几十个非技术用户分发代码更新是一个行政噩梦
In short:

Is there any way to run Python WITHOUT trying to create .pyc files (or
.pyo) or to have Python not attempt to import the .pyc files it finds?

Reason:

We have a site-specific package installed on a network drive[1]. When
anyone with write access imports this package, the network drive gets
spammed with .pyc files.
Well, at least they have the right to create them. Wouldn''t it be
easier, though, to just create all the .pyc files.
If these .pyc files exist, they appear to cause problems when other
users'' Python interpreters use them instead of the .py files. (I know,
they *should* work, but they don''t). This may have something to do
with the fact that all of these users (on Windows) have the network
drive mapped to arbitrary drive letters. I don''t know.
The .pyc file is normally created in the same directory as the .py file.
You may be seeing problems because one user doesn''t have permissions to
access a file created by another. In that case you may be able to set
the inherited permissions for Creator Owner to allow reading by all users.

You might also expect problems if one user was using python 2.2 and
another was using 2.4, since each version requires its own format for
the .pyc file, and they might conflict. Ulitmately it sounds like a
permissions problem.

PEP 304 would have helped, but it appears to be deceased. What I
really want is a command line option
Not sure it''s deceased (a dead parrot?) - it''s on the standards track,
it hasn''t been rejected, and Skip has actually provided a patch to
implement the solution.
I''m going to have to cobble together a work-around, like having
imported modules delete their own .pyc files immediately after import,
but it would be really nice to have a /good/ way of not using .pyc...
It would be *much* more sensible to find the underlying cause of the
problems and actually fix them :-)
(footnotes)

[1] Because it''s an administrative nightmare to distribute code updates
to dozens of non-technical users



我听到了。


问候

Steve

-

Steve Holden +1 703 861 4237 +1 800 494 3119

Holden Web LLC http://www.holdenweb.com/

Python网页编程 http://pydish.holdenweb.com/


I hear that.

regards
Steve
--
Steve Holden +1 703 861 4237 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/


Steve Holden写道:
Steve Holden wrote:
Lonnie Princehouse写道:
Lonnie Princehouse wrote:
如果存在这些.pyc文件,当其他用户的Python解释器使用它们而不是.py文件。 (我知道,
他们*应该*工作,但他们不是)。这可能有一些事情要做,因为所有这些用户(在Windows上)都将网络
驱动器映射到任意驱动器号。我不知道。
If these .pyc files exist, they appear to cause problems when other
users'' Python interpreters use them instead of the .py files. (I know,
they *should* work, but they don''t). This may have something to do
with the fact that all of these users (on Windows) have the network
drive mapped to arbitrary drive letters. I don''t know.



这不是原因。
如果一个用户使用python 2.2和<你可能也会遇到问题另一个是使用2.4,因为每个版本都需要自己的.pyc文件格式,它们可能会发生冲突。 Ulitmately听起来像是一个
权限问题。
找到问题的根本原因并实际修复它们会更加明智: - )


That won''t be the reason.
You might also expect problems if one user was using python 2.2 and
another was using 2.4, since each version requires its own format for
the .pyc file, and they might conflict. Ulitmately it sounds like a
permissions problem. It would be *much* more sensible to find the underlying cause of the
problems and actually fix them :-)




我同意史蒂夫,并希望添加他给你的东西。


.pyc文件写入的唯一原因是内部的幻数

与您的python版本所期望的不匹配,或者内部存储的

时间戳与.py

文件的时间戳不匹配它是从哪个创建的。据我所知,如果这两件事都是真的(如果文件实际上是可读的)那么.pyc文件加载了
并忽略了.py ,并没有写新的.pyc文件。


我建议你挑选两个拥有相同版本python的用户和

测试你的.pyc文件一次又一次地写。如果他们不是b $ b,那么就开始寻找坏了。用户可能有一个旧版本的
版本的python。如果他们一遍又一遍地写,那么史蒂夫可能是关于权限问题的确定。


HTH

-Peter



I agree with Steve, and want to add to what he''s given you.

The only reason a .pyc file gets written is if the magic number inside
doesn''t match the one expected by your version of python, or if the
timestamp that is stored inside doesn''t match the timestamp of the .py
file from which it was created. As far as I know, if both these things
are true (and if the file is actually readable) then the .pyc file is
loaded and the .py is ignored, and no new .pyc file is written.

I suggest you pick two users who have the same version of python and
test whether your .pyc files are written again and again. If they are
not, then start looking for the "bad" user who probably has an older
version of python. If they are written over and over, then Steve is
probably correct about a permissions issue.

HTH
-Peter


这篇关于有什么办法不创建.pyc文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆