I have implemented a package names myUtils, it consists of folder 'myUtils', file 'init.py' and a number of *.py files with names != 'myUtils'. This package is included in myOtherProject.py and can be found/used when I run them from Eclipse.
However, when I run py2exe on myOtherProject.py, resulting exe cannot find this module(error message "ImportError: no module named myUtils"). Trimmed version of my setup.exe:
from distutils.core import setup
import py2exe, sys
sys.path.append(pathTo_myUtils)
import myUtils # this line works fine even if I comment out sys.path.append(...)
data_files_ = (('.', ["C:\\Python27\\DLLs\\MSVCP90.dll",
"C:\\Python27\\lib\\site-packages\\Pythonwin\\mfc90.dll"]))
setup(windows=['myOtherProject.py'], options={'py2exe': {'excludes': ['tcl'], 'includes': ['myUtils'], 'dll_excludes': ['tk85.dll', 'tcl85.dll'] }}, data_files=data_files_)
How could I fix this? I am using Python 2.7 on WinXP.
put your sys.path.append() line BEFORE the import statement. Better yet, modify your PYTHONPATH (i'm not sure how to do this on windows, but i'm sure Google can tell you how)
I did not define PYTHONPATH properly; there were spaces after semicolons. Instead of
c:\aa\; c:\bb\; c:\cc\
it needed to be
c:\aa;c:\bb;c:\cc
For packages that are defined using init.py (package MyPackage corresponds to a folder MyPackage, that contains init.py and some other files, without MyPackage.py), path that I needed to add to PYTHONPATH was not
<path_to_MyPackage>\MyPackage
but just
<path_to_MyPackage>
...
Related
I have a project that includes c++ binaries and python scripts, it's setup such that it should be installed using setuptools. One of the python files is intended to be both used as a script "
python3 script_name.py params
and for it's primary function to be used in other python projects from script_name import function.
The primary function calls a binary which is in a known relative location before the installation (the user is expected to call pip install project_folder). So in order to call the binary I want to get this files location (pre installation)
To get this I used something like
Path(__file__).resolve().parent
however, since the installation moves the file to another folder like ~/.local/... this doesn't work when imported after the installation.
Is there a way to get the original file path, or to make the installation save that path somewhere?
EDIT:
After #sinoroc 's suggestion I tried including the binary as a resource by putting an __init__.py in the build folder and putting
from importlib.resources import files
import build
binary = files(build).joinpath("binary")
in the main init. After that package.binary still gives me a path to my .local/lib and binary.is_file() still returns False
from importlib_resources import files
GenerateHistograms = files("build").joinpath("GenerateHistograms")
gave the same result
Since you are installing your package, you also need to include your C++ binary in the installation. You cannot have a mixed setup. I suggest something like this.
In your setup.py:
from setuptools import setup, find_packages
setup(
name="mypkg",
packages=find_packages(exclude=["tests"]),
package_data={
"mypkg": [
"binary", # relative path to your package directory
]
},
include_package_data=True,
)
Then in your module use pkg_resources:
from pathlib import Path
from pkg_resources import resource_filename
# "binary" is whatever relative path you put in package_data
path_to_binary = Path(resource_filename("mypkg", "binary"))
pkg_resources should be pulled in by setuptools.
EDIT: the recipe above might be a bit out of date; as #sinoroc suggests, using importlib.resources instead of pkg_resources is probably the modern equivalent.
So I managed to solve it in with #sinroc 's approach in the end
In setup.py
package_data={'package':['build/*']}
include_package_data=True
and in the primary __init.py__:
from importlib.resources import files
binary = files("package.build").joinpath("binary")
So I could then from package import binary to get the path
EDIT: Looks like someone also pointed the error in my ways out before I finished ^^
I have a large project with multiple packages. These packages use a set of modules in a common package. I am trying to create an exe on Windows using pyinstaller, but it cannot find the common package.
This cut down project exhibits the same issue. My package is organised as shown in this tree:
When I use
python -m my_package
in the top my_package directory it works perfectly.
The module main.py in my_package imports Bar (which is located in foo) from common. The __init__.py file in common includes:
from common.source.foo import Bar
When I build and exe file and run it in terminal, it fails with ' No module named common'
my pyintstaller spec includes:
hiddenimports=['../', '../common/', '../common/common/']
Should I try something different?
The hiddenimports are used to specify imports that can't be detected by pyinstaller, not the paths to those imports.
Try adding the necessary paths to the pathex list in the spec file instead (these are paths that will be available in sys.path during analysis).
So... I am attempting to teach myself Python.
In such, I am attempting to build something that I appear to have no clue about...
I have a "workingdir" structure such as:
/
-- classes/
-- -- install
-- myfile
In myfile I am simply attempting to "import" the file install by using:
import classes.install
Which fails with: ImportError: No module named 'classes.install'
I have attempted the following as well, and all end the same way, with the same error:
import .classes.install
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
import classes.install
As well as putting an empty __init__.py file inside the classes directory
the file install simply contains:
class gyo_install():
inst = False
# check if we have everything we need installed.
def __init__():
print("Hello World")
What am I doing wrong? I've searched and searched and searched, everything I see points to the same solutions I've attempted, and none of them work.
Python looks for files with a .py extension when importing modules. So a file named myfile will not be recognized simply by the command import myfile. The pythonic way to ensure that the interpreter will find the module is to ensure it has a .py extension. Renaming myfile to myfile.py and install to install.py and then changing the import command to
import classes.install
should solve the problem.
Create __init__.py inside install directory.
Explanation: You can import from a file that is in your current directory or from a package. A package is a directory with __init__.py inside. In fact, a package can contain only this single file.
You can read the documentation for further information.
I am new to windows and I am working on setting up code and modules with visual studio.
I have the following folder structure for my code:
myModule
__init__.py
mymodule.py
myScript
myscript.py
In myscript.py I have the following command:
from myModule import *
In visual studio this command works, but when I run the script command line I get the following error:
ModuleNotFoundError: No Module named myModule
Is there a quick trick in windows to do the job w/o having to install myModule as a package?
Any of the usual tricks that work in linux don't seem to work in windows. I.e.,
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'myModule'))
or,
import ..myModule
The path should have entries that point to the module. Your path entry is pointing inside the module.
What you had:
os.path.join(os.path.dirname(__file__), r"..", 'myModule')
Instead, you want to point to the directory containing myModule, which would be:
os.path.join(os.path.dirname(__file__), r"..")
I am trying to import the module
import QSTK.qstkutil.qsdateutil as du
But I get the Error
ImportError: No module named QSTK.qstkutil.qsdateutil
My current working directory is
'c:\\Python27\\Lib\\site-packages\\QSTK'
and in the path C:\Python27\Lib\site-packages\QSTK\qstkutil there are the files
qsdateutil.py
qsdateutil.pyc
qsdateutil.pyo
Does importing QSTK work?
import QSTK
How about QSTK.qstkutil?
If not this is most likely a sys.path problem. Please post the result of:
>>>import sys
>>>sys.path
It should look like:
[ [...], 'C:\Python27\Lib\site-packages', [...] ]
Another thing you can check, is if 'C:\Python27\Lib\site-packages\QSTK\qstkutil' contains a file named '__init__.py'. From the module documentation:
The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later.
try a fresh installation and make sure you run sudo python setup.py install , command after unpack-aging , QSTK. that process links QSTK.qstkutil.qsdateutil.