How can we get the compiled python pyc file path with python 3.x in the current environment? I know it's in the __pycache__ direcotry, but I couldn't find a way to find the file path. Because the names of pyc files of python 3 changes by the environment.
Given that you know the path to the source (ie .py) file, there's a function importlib.util.cache_from_source that does exactly what you want. For example, to get the .pyc file corresponding to the numpy package, you would do:
import importlib
import numpy
importlib.util.cache_from_source(numpy.__file__)
For me (on OS X), this prints out something along the lines of:
/<absolute path to site-packages>/numpy/__pycache__/__init__.cpython-36.pyc
Related
So I builded a python package localy:
cgi#cgires:~$ pip list | grep mads
madscgi 0.1.0
Its nice! Afterwards I can use it in Jupyter Notebook, in iPython Shell, in Python Shell and even in python scripts outside the modules code. So it works as expected 100% outside the modules code:
Thats nice, but next I want to import code from one builded module (inside the package) into another python file (inside the package). Lets name it import_test.py and try it out:
So it fails if it is getting executed in the directory, where the package is build from. And it looks like, that the python interpreter is taking the parent directory (with the same name like the module) and this is failing.
Is is possible to enforce the usage of the installed pip-package?
As #MisterMiyagi pointed out, the problem was, that there were an upper folder which had the same name as the module.
Here: mads_cons is the upper folder from import_test.py. Therefore, the upper folder is getting imported instead of the via pip installed module. Thats it.
The file you want to import should either be in the same folder or referred to with the absolute path of it.
If that doesn't suit you, you can call sys.path
import sys
sys.path
You can keep your file in any of the directories sys.path returns.
Smart would be, if you keep the file inside.
......../site-packages/
The python3 version is Python 3.5.3 in my os.
mkdir workspace
cd workspace
vim print.py
print("i am learning")
Saved and exit.
python3 print.py
i am learning
As far as i knew, python source file was parsed and compiled into pyc file when to execute it.
ls
print.py
There is no pyc file in workspace directory,where is the complied print.py file then?
sudo find / -name ".pyc"
The find command still can't search pyc file such as print.pyc .
python3 -m compileall can create the compiled file for print.py manually,where is the compiled file for print.py created by python itself?
Does python3 delete the print.pyc after executing python3 print.py?
Ok this is one big of a problem I ever had when I'm started to learn python few years back. Python is just like any other oop programming languages which does compilation before program execution. When python compiles its program, it creates the bite code which is you can see by standard library called dis.
import dis
print(dis.dis(your_program))
Sometimes (not always) python creates .pyc file for the running programs to improve the speed up the loading of import modules but not to improve the execution time. So hope you get intuition behind .pyc, furthermore .pyc only creates when your module is import by another module.
As an example, Imagine you have this print.py (Let's modify it shall we)
def return_print_statment(statement):
print('Printed version: ', statement)
Suppose this module imported by another custom module called views.py. In views.py there is a module_view which will use the return_print_statment
from print import return_print_statment
def module_view():
...
return_print_statment(output)
So in the compilation, since you have imported the print.py python will generate print.pyc file for it. In python 2.0 python will put the .pyc to right next to your program in the same folder, but in python3 instead of creating in the same folder python will create separate folder called __pycache__ in the same directory to put these byte codes.
python3 -m compileall .
To compile all .py files in your current directory.
http://effbot.org/pyfaq/how-do-i-create-a-pyc-file.htm
How to extract a zip file using python when zip file present in different directory where script file present.
I try this ,but i got error because source path is not accepted ,try to solve me this problem.
from zipfile import ZipFile
def func(source, target):
with ZipFile('source', 'target'):
ZipFile.Extractall('target')
Use this code. To move through directories you could either hard code the directory where your script is present or you could toggle through the directories using simple commands such as "../" to move out of the given directory or "/" to move inside a folder in the directory. For example - "../script.py" or "/folder/script.py". Similarly you can use this to find your .zip file.
import zipfile
with zipfile.ZipFile("file.zip","r") as zip_ref:
zip_ref.extractall("targetdir")
For just unpacking, shutil should suffice:
import shutil
shutil.unpack_archive('path-to-zipfile')
You'll have to check for source path of the zip file which is relative to your current working directory. To know your current working directory you can try
import os
print(os.getcwd())
zip - Unzipping files in python
relative-paths-in-python
I'm a bit new to Python and I'm building a project in PyCharm since I'm used to IntelliJ, and have a problem with my file structure. When I need to import a file in the same subdirectory-- directory c, I have to type
from a.b.c import y where a is the project's home directory b.c is the sub directory I'm in.
So I'm not able to import y directory. Which this then causes a problem if I want to run this file via command line, it uses the current directory as the path, meaning the import doesn't know anything about x.xx. What can I do to fix this issue?
Thanks!
For the purposes of this answer I'm assuming y is a Python module in the c directory. In other words there is a file called y.py in a/b/c.
import y works in a Python module in directory c if the current working directory is also c.
In Intellij IDEA with the Python plugin (much the same as PyCharm most of the time) the current working directory is called the "Working Directory" in each Run/Debug configuration you set up to run your script.
import y will also work if c is on the PYTHONPATH.
The other way to make y available to the import statement is to turn a and b directories into Python packages. That means at least putting an empty __init__.py file in both the a and b directories.
You can then use a as the root directory for the project, and use:
from a.b.c import y
It's worth reading The Definitive Guide to Python import Statements if you're not sure how Python resolves imports.
This is my directory structure
-directory1
|-currentlyWritingCode
-directory2
|-__init__.py
|-helper.py
|-view.ui
The helper.py is a UI from pyqt4 framework. It needs the view.ui file .
it has following code to load the ui data
viewBase, viewForm = uic.loadUiType("view.ui")
now in directory1 in the currently writing code I did this
import directory2.helper
when I run the code in currentlyWritingCode it is throwing an error that
FileNotFoundError: [Errno 2] No such file or directory: 'view.ui'
What to do now?
using python3.5 from anaconda on Centos 7
Use os.path.join(os.path.dirname(os.path.realpath(__file__)),'view.ui') in place of view.ui. This will ensure you correctly reference the folder that the python file lives in, regardless of the code that imports it.
Note: Make sure you have import os with your other imports.
How does this work?
__file__ is an attribute of the module you are importing. It contains the path to the module file. See this answer. However, this path is not necessarily an absolute path. os.path.realpath returns the absolute path (it even follows symlinks if there are any). At this point we have a full path to the module, so we take the path to the directory (os.path.dirname) and join it with the original filename (which we assumed to be relative to the original module and so should be in the aforementioned directory). os.path.join ensures that the correct \ or / is used when constructing a file path so that the code works on any platform.