I can't find any info on this but when I try and create a zip archive in Python it creates a .pyc instead.
#!/Python27/python
import zipfile
z = zipfile.ZipFile('test.zip', 'w')
z.write('README.txt')
z.close()
This is the result of running the script.
The script you're running (or some other script you have) is actually called zipfile.py, and so Python is actually first looking in the folder your script is in to find a module called zipfile. When it finds this script, it imports that instead of the actual module.
Any time a script is imported to another Python file, Python automatically creates a compiled .pyc file resulting in that zipfile.pyc. If you rename your file to something more specific (and also fix your typo) you should be able to avoid this problem.
Related
I want to have 2 copies of my python script, one as .py extension and the other as .pyw extension (Windows version, no console)
That allows me to run the .py file and get output from console (from command line) and associate the .pyw file to graphical programs.
Of course, the worst option is to duplicate files, which works but maintainability suffers.
When I was using clearcase, I used to create a symlink from one file to another but symlinks aren't the best choice when managing a code base.
I could create a helper module that I import in both cases to share the maximum of source code, but I have a lot of scripts in that case, and I'd like a generic solution.
What's the alternative to copying the whole file?
Here's a generic solution that doesn't depend on the contents of the .py file.
Say I have a foo.py file and I want to create a foo.pwy with the exact same behaviour, let's create this foo.pyw file with this exact contents in the same directory:
import os
with open(os.path.splitext(__file__)[0]+".py") as f:
contents = f.read()
exec(contents)
As one can see, the name foo is nowhere to be seen in the contents. The script takes the name of the script, changes the extension to .py, reads it and executes it.
With some simple scripting, one can easily replace all copies of .pyw files to reference the .py files with this exact content no matter the contents of the original .py file.
This saved a lot of hassle when we migrated from Clearcase to git and had to drop the symbolic links.
My python module needs to save some configuration files into a disk, is there a standard convention on where to save your module configuration files? one that you won't run into a permission error.
You can store these configuration file where ever you want (as long as your python script have the write permission to this directory and no process prevent it from writing to this directory (is not locked by other process)), as long as your module can reach them for parsing or for import if they are a python files.
what are the options:
store them in the same module directory, and import/parse them.
store them in deferent directory and in your python module change the os.path with os.chdir(path) to this dir for parsing.
store them in dir_x and provide the dir_x path to your python script via command line args with sys.argv[1]. or with argparse!
I recently compiled my first python program into a standalone executable. There is one problem however. My python program originally accessed a .json file within the same directory and wrote contents to it. Now that I have made my python file into an exec file, it does not seem to access the .json file in the same directory anymore. Please help!
I guess the problem occurs because the the executable is not in the same folder as your script.
I can suggest you this solution:
import os
# csfp - current script folder path
csfp = os.path.dirname(os.path.realpath(__file__))
json_file = os.path.join(csfp, "config.json")
And you do whatever you want to do with this json_file variable, that contains the path and the name of your json file (example C:\Some_folder\My_python_project\config.json)
I seen the some difference when I execute the .py file. I have observed two cases,
1) when I run the .py file using the python mypython.py
I got the result. But .pyc file not created in my folder.
2) when I run the .py file using the python -c "import mypython"
I got the same result. But .pyc file was created in my folder.
My question is why first case not created .pyc file ?
Import is generally used when you need to use the contents of a file in another script or program, see What does python file extensions, .pyc .pyd .pyo stand for?. So to more specifically answer the question, the .pyc is created to ease access to the contents of the file in the future and is only created when the import command is used.
Python saves the precompiled .pyc file only for imported modules, not for the main script you're running.
Running a program as main or importing it as a module is not the exact same thing, but very similar because in a module everything that is at top level is executed at import time.
Note that for main program the source code is completely parsed and compiled too (so for example if you have a syntax error in last line nothing will be executed). The difference is only that the result of compilation is not saved back to disk.
This might be a weird requirement but it's what I've run into. I Googled but yield nothing.
I'm coding an application who's using a lot of constant attributes / values recorded in an XML file (they'll not change so a static file), things work fine until I generated an egg file for it.
When the logic reaches the XML accessing part, I got one complaint like this:
/home/Workspace/my_proj/dist/mps-1.2.0_M2-py2.6.egg/mps/par/client/syntax/syntax.xml
Actually I've bundled the XML file in the path above but seems Python doesn't know how to access it.
The code to access the XML is as...
file_handler = open(path_to_the_file)
lines = file_handler.read().splitlines()
Any idea?
egg files are zipfiles, so you must access "stuff" inside them with the zipfile module of the Python standard libraries, not with the built-in open function!
If you want to access the contents inside the .egg file you can simply rename it and change extension from .egg to .zip and than unzip it.
Which will create a folder and the contents will be same as they were when it was a .egg file
for example brewer2mpl-1.4.1-py3.6.egg
After Renaming brewer2mpl-1.4.1-py3.6.zip
Now if we open it, it'll get easily unzipped and the content will be put in a folder with same name in the same directory.
(tested on macOS Sierra)
The less command on *nix systems can peek inside zip files. Therefore less some.egg will list the contents of a .egg file too.
Just run unzip file.egg
You can install unzip on Debian/Ubuntu with
sudo apt install unzip
or on macOS by installing Homebrew then
brew install unzip
Access file from inside egg file
Yes, It is possible to read the files from inside egg file.
Egg file: mps-1.2.0_M2-py2.6.egg structure for module level example:
In driverfile.py:
import xml.etree.ElementTree
import mps.par.client as syntaxpath
import os
path = os.path.dirname(syntaxpath.__file__)
element = xml.etree.ElementTree.parse(path+'\\syntax\\syntax.xml').getroot()
print(element)
Read xml file from inside an eggfile:
PYTHONPATH=mps-1.2.0_M2-py2.6.egg python driverfile.py
i think by default eggs packing file under python won't add your xml inside to the pack