I'm unable to add dll files using package_data in setup.py file. Here's a look at directory structure:
my_project
├── key
│ ├── 1_0
│ │ ├── sub_dir
│ │ │ ├── _required.dll
│ │ │ └── __init__.py
│ │ ├── get_key.py
│ │ └── __init__.py
│ ├── 1_1
│ │ ├── sub_dir
│ │ │ ├── _required.dll
│ │ │ └── __init__.py
│ │ ├── get_key.py
│ │ └── __init__.py
│ ├── 1_2
│ │ ├── sub_dir
│ │ │ ├── _required.dll
│ │ │ └── __init__.py
│ │ ├── get_key.py
│ │ └── __init__.py
│ └── __init__.py
├── my_program.py
└── __init__.py
I've been trying without success to add the required.dll files with the installation of this module. I know I have to add it in the setup.py file. What I've tried so far (I'll skip all unnecessary parameters) :
First:
setuptools.setup(name='my_project',
packages=setuptools.find_packages(),
include_package_data=True,
package_data={'': ['my_project\\key\\1_0\\sub_dir\\_required.dll',
'my_project\\key\\1_1\\sub_dir\\_required.dll',
'my_project\\key\\1_2\\sub_dir\\_required.dll']},
...)
Second:
setuptools.setup(name='my_project',
packages=setuptools.find_packages(),
include_package_data=True,
package_data={'key': ['1_0\\sub_dir\\_required.dll',
'1_1\\sub_dir\\_required.dll',
'1_2\\sub_dir\\_required.dll']},
...)
Third:
setuptools.setup(name='my_project',
packages=setuptools.find_packages(),
include_package_data=True,
package_data={'my_project\\key\\1_0\\sub_dir': ['_required.dll'],
'my_project\\key\\1_1\\sub_dir': ['_required.dll'],
'my_project\\key\\1_2\\sub_dir': ['_required.dll']}
...)
Whenever I call python setup.py sdist --format=zip, dll files are never included. BTW, I'd rather not change directory structure, unless there is no other option.
What am I missing here?
Regards.
François
Related
I want to copy images from multi subfolders into only one folder using python or any library that can do with python framework
my folders as described in tree below
$ tree
.
├── main_folder
│ ├── Subfolder_1
│ │ └── Subfolder1_1
│ │ └── ├── 0.png
│ │ ├── 1.png
│ │ ├── 2.png
│ │ └── 3.png
│ │ └── Subfolder1_2
│ │ └── ├── 4.png
│ │ ├── 5.png
│ │ ├── 6.png
│ │ └── 7.png
.....
│ ├── Subfolder_2
│ │ └── Subfolder2_1
│ │ └── |____.png
│ │ ├── 8.png
│ │ └── 9.png
│ │ └── Subfolder2_2
│ │ └── ├── 10.png
│ │ ├── 11.png
│ │ ├── 12.png
│ │ └── 13.png
│ └── Subfolder_3
│ └── Subfolder3_1
│ └── |___ .png
│ ├── 14.png
│ ├── 15.png
│ ├── 16.png
│ │ └── Subfolder3_2
│ │ └── ├── 17.png
│ │ ├── 18.png
│ │ ├── 19.png
│ │ └── 20.png
│
└── script.py
The expected results destination_folder
will look like the tree below
── destination_folder
├── 0.png
├── 1.png
├── 2.png
└── 3.png
.........
├── n-1.png
└── n.png
just use a mixture of glob for finding files and shutil for copying files.
import glob
import os
import shutil
dest_folder = 'destination_folder'
if not os.path.isdir(dest_folder):
os.mkdir(dest_folder)
for item in glob.glob('**/*.png',recursive=True):
filename = os.path.basename(item)
full_path = os.path.abspath(item)
shutil.copy(full_path, os.path.join(dest_folder,filename))
if you only want pictures with numbers you can add an if condition to it
for item in glob.glob('**/*.png',recursive=True):
filename = os.path.basename(item)
if filename.split('.')[0].isdigit():
full_path = os.path.abspath(item)
shutil.copy(full_path,os.path.join(dest_folder,filename))
I have the following folder structure:
...
│
├── src
│ ├── folder_A
│ │ └── file_A.py
│ │ └── __init__.py
│ │
│ ├── folder_B
│ │ └── file_B.py
│ │ └── __init__.py
│ │
│ └── __init__.py
│
│
└── something else
In the file file_A.py I put from folder_B import file_B as fb. But file_A.py works only in debug mode (meaning that the code produces the expected results). If I run file_A.py in the standard way I get the error ModuleNotFoundError: No module named 'folder_B'.
I also changed the configuration before running the code, putting C:\Users\***\***\***\src as the working directory of file_A.py but it still doesn't work.
What can be a solution?
If your current directory is src, then folder_B is in the path because of that. If you want to make a package with sub-packages that can access each other, place everything into a root package:
│
├── src
│ └── root_package
│ ├── folder_A
│ │ ├── file_A.py
│ │ └── __init__.py
│ │
│ ├── folder_B
│ │ ├── file_B.py
│ │ └── __init__.py
│ │
│ └── __init__.py
│
└── something else
Now in file_A, you can do
from ..folder_B import file_B as fb
Since src is not a package, you can't do a relative import through it. By adding root_package, you make it possible to find folder_B in the same package hierarchy (albeit a different branch) as the module doing the import.
Im writing a library for internal use,its called "etllib", and I have the following structure:
etl-lib
├── README.md
├── etllib
│ ├── __init__.py
│ ├── client
│ │ ├── __init__.py
│ │ ├── elastic.py
│ │ └── qradar.py
│ ├── etl
│ │ ├── __init__.py
│ │ └── etl_imperva.py
│ └── util
│ ├── __init__.py
│ ├── config.py
│ ├── daemon.py
│ ├── elastic
│ │ ├── __init__.py
│ │ └── impeva_index_config.py
│ └── imperva
│ ├── __init__.py
│ ├── kpe_config.py
│ └── query_config.py
├── scripts
│ └── etl_imperva
└── setup.py
And I have a script called "etl_imperva" in etllib/scripts. The code inside looks like this :
#!/usr/bin/python3
import sys
from etllib.etl.etl_imperva import ETL
# Run with python3 imperva_run.py start|run|stop|restart
ETL.startup(sys.argv)
If I install this package(etllib) and call this script, it works just fine. But when I need to test stuff, how can I tell python to use the modules that are on my working directory instead the ones are installed? Because each time I make a change on the modules, I need to reinstall the package and this is a little time consuming.
I also tried uninstalling the package for testing, but whe I run this script I get the following error :
Exception has occurred: ModuleNotFoundError
No module named 'etllib'
File "/home/jleonse/etl-lib/scripts/run_imperva", line 3, in <module>
from etllib.etl.etl_imperva import ETL
Is there a better way to do this?
Actually, it is not on the same level in the hierarchy.
from etllib.etl.etl_imperva import ETL
would work only if etllib was in the same directory or in a directory in your system PATH, but the etllib is in the parent directory, hence it can not find it.
so you can make it work if you change the project structure to be:
etl-lib
├── README.md
├── etllib
│ ├── __init__.py
│ ├── client
│ │ ├── __init__.py
│ │ ├── elastic.py
│ │ └── qradar.py
│ ├── etl
│ │ ├── __init__.py
│ │ └── etl_imperva.py
│ └── util
│ ├── __init__.py
│ ├── config.py
│ ├── daemon.py
│ ├── elastic
│ │ ├── __init__.py
│ │ └── impeva_index_config.py
│ └── imperva
│ ├── __init__.py
│ ├── kpe_config.py
│ └── query_config.py
├── etl_imperva
│
└── setup.py
In linux, my folder looks like that:
src
├── app
│ ├── app.component.css
│ ├── app.component.html
│ ├── app.component.spec.ts
│ ├── app.component.ts
│ ├── app.module.ts
│ ├── app-routing.module.ts
│ ├── components
│ │ ├── catalog-main
│ │ │ ├── catalog-main.component.css
│ │ │ ├── catalog-main.component.html
│ │ │ ├── catalog-main.component.spec.ts
│ │ │ └── catalog-main.component.ts
│ │ ├── catalog-sidebar
│ │ │ ├── catalog-sidebar.component.css
│ │ │ ├── catalog-sidebar.component.html
│ │ │ ├── catalog-sidebar.component.spec.ts
│ │ │ └── catalog-sidebar.component.ts
│ │ └── top-bar
│ │ ├── top-bar.component.css
│ │ ├── top-bar.component_handy.css
│ │ ├── top-bar.component_handy.html
│ │ ├── top-bar.component.html
│ │ ├── top-bar.component.spec.ts
│ │ └── top-bar.component.ts
│ ├── icons-provider.module.ts
│ ├── pages
│ │ ├── login
│ │ │ ├── login.component.css
│ │ │ ├── login.component.html
│ │ │ ├── login.component.spec.ts
│ │ │ ├── login.component.ts
│ │ │ ├── login.module.ts
│ │ │ └── login-routing.module.ts
│ │ ├── monitor
│ │ │ ├── monitor.component.css
│ │ │ ├── monitor.component.html
│ │ │ ├── monitor.component.spec.ts
│ │ │ ├── monitor.component.ts
│ │ │ ├── monitor.module.ts
│ │ │ └── monitor-routing.module.ts
│ │ ├── monitor2
│ │ │ └── monitor2.module.ts
│ │ └── welcome
│ │ ├── welcome.component.css
│ │ ├── welcome.component.html
│ │ ├── welcome.component.ts
│ │ ├── welcome.module.ts
│ │ └── welcome-routing.module.ts
│ ├── reducers
│ │ └── index.ts
│ └── store
│ ├── catalog.actions.ts
│ ├── catalog.reducer.ts
│ └── userinfo.ts
├── assets
├── environments
│ ├── environment.prod.ts
│ └── environment.ts
├── favicon.ico
├── index.html
├── main.ts
├── polyfills.ts
├── styles.css
├── test.ts
└── theme.less
I use this command output 29 files:ll src/**/*.ts | wc -l
I use this command output 32 files:ll src/{**,}/*.ts | wc -l, new files are src/main.ts, src/test.ts, src/polyfills.ts, they are direct in src folder
But in python3,
I use glob.glob("src/**/*.ts") output all 32 files,
I use glob.glob("src/{**,}/*.ts") output all 0 files
So, which is standard glob syntax?
which is standard glob syntax?
Because you tagged linux I assume you use bash. Bash has nostandard (ie. it's not in posix) extension not related to globbing brace expansion.
The:
ll src/{**,}/*.ts | wc -l
is first expanded to:
ll src/**/*.ts src/*.ts | wc -l
by bash when doing brace expansion. Then next happens globbing expansion. Python does not do any shell expansions when parsing glob arguments, so python searches for files/directories named { and ending with , and } taken literally as characters - as it finds none, the count is 0.
So you are not comparing globbing, you are comparing globbing with shell expansion with only globbing. You can compare your commands with bash with brace expansions disabled set +B or use another shell without brace expansions, like busyboxs.
Neither. The man page for the POSIX function glob(3) allows neither syntax.
** is an extension, invented and first popularised by zsh. Bash and some others copied it and generally did something slightly incompatible. Bash and Python both copied it, but chose slightly different behaviour when interacting with other globbing syntax.
This makes sense: After all, while glob(3) is a POSIX function, precisely specified and to be implemented exactly as specified, zsh is merely a role model, from one one can deviate at will. And one has to deviate, because zsh has a lot of globbing extensions.
I have the following directory structure in my home projects folder.
|ALL-IN-ONE
|demo
|__init__.py
|__main__.py
|models
|grpc
|allinone_server.py
And I want to import from allinone_server.py a function defined in main.py called images_demo. I have tried
from demo.__main__ import images_demo
It is not working. How can I import it? The function I am trying to import is located inside main.py which is inside demo directory. I am trying to import it from the file allinone_server.py in grpc. I guess I have made my question clear now.
Here is the whole tree for the project
├── demo
│ ├── __init__.py
│ ├── __main__.py
│ └── __pycache__
│ ├── __init__.cpython-36.pyc
│ └── main.cpython-36.pyc
├── description
├── environment.yml
├── HEAD
├── hooks
│ ├── applypatch-msg.sample
│ ├── commit-msg.sample
│ ├── fsmonitor-watchman.sample
│ ├── post-update.sample
│ ├── pre-applypatch.sample
│ ├── pre-commit.sample
│ ├── prepare-commit-msg.sample
│ ├── pre-push.sample
│ ├── pre-rebase.sample
│ ├── pre-receive.sample
│ └── update.sample
├── imgs
│ └── 44.jpg
├── info
│ └── exclude
├── __init__.py
├── loggers
│ ├── __init__.py
│ └── __pycache__
│ └── __init__.cpython-36.pyc
├── models
│ ├── adience_large1.h5
│ ├── adience_small1.h5
│ ├── AgeModel.json
│ ├── detection_age_gender_large1.h5
│ ├── detection_age_gender_small1.h5
│ ├── detection_age_gender_smile_large1.h5
│ ├── detection_age_gender_smile_small1.h5
│ ├── detection_age_large1.h5
│ ├── detection_age_small1.h5
│ ├── detection_large1.h5
│ ├── detection_small1.h5
│ ├── grpc
│ │ ├── adele_2016.jpg
│ │ ├── allinone_client.py
│ │ ├── all_in_one_pb2_grpc.py
│ │ ├── all_in_one_pb2.py
│ │ ├── all_in_one.proto
│ │ ├── allinone_server.py
│ │ ├── benedict_cumberbatch_2014.png
│ │ ├── cat.png
│ │ ├── classroom_in_tanzania.jpg
│ │ ├── decoded1.py
│ │ ├── decoded.py
│ │ ├── elon_musk_2015.jpg
│ │ ├── laos.jpg
│ │ ├── model_face.jpg
│ │ ├── __pycache__
│ │ │ ├── all_in_one_pb2.cpython-36.pyc
│ │ │ ├── all_in_one_pb2_grpc.cpython-36.pyc
│ │ │ └── decoded.cpython-36.pyc
│ │ ├── sophia.jpg
│ │ ├── test
│ │ │ ├── __init__.py
│ │ │ ├── __pycache__
│ │ │ │ └── __init__.cpython-36.pyc
│ │ │ └── test_images
│ │ │ ├── adele_2016.jpg
│ │ │ ├── benedict_cumberbatch_2014.png
│ │ │ ├── classroom_in_tanzania.jpg
│ │ │ ├── elon_musk_2015.jpg
│ │ │ ├── __init__.py
│ │ │ ├── laos.jpg
│ │ │ ├── model_face.jpg
│ │ │ ├── sophia.jpg
│ │ │ ├── waaah.jpg
│ │ │ ├── woman.jpg
│ │ │ └── zebra_stripes.jpg
│ │ ├── waaah.jpg
│ │ ├── woman.jpg
│ │ └── zebra_stripes.jpg
So you've referred to main.py, but you also have __main__.py in your directory structure. I'll assume that your directory actually contains main.py instead of __main__.py.
To import from levels up in a package, start your import with a period.
To import just one function you would use from .main import images_demo
Now, let's start by saying main.py is in grpc/ along with allinone_server.py, then we'll move it to different directories and see how the import changes.
If it were in grpc/ from .main import images_demo
If it were in models/ from ..main import images_demo
If it were in __ALL-IN-ONE/ from ...main import images_demo
If it were in __demo/ from ...__demo.main import images_demo
Every extra period brings you up one level in the hierarchy, then you use the name of the next level down in the target path until you reach where you want to be.
Now let's suppose you wanted to import the whole of main.py.
If it were in grpc/ from . import main
If it were in models/ from .. import main
If it were in __ALL-IN-One/ from ... import main
If it were in __demo/ from ...__demo import main
Finally, the dot notation to move up a level only works if the file that uses it is in a package, so this will work fine if at the top level you start your program in a scope outside of this package then use from __ALL-IN-ONE.models.grpc import allinone_server
However, if you run allinone_server.py directly then it will fail to import anything above it as it isn't being imported as part of a package. Try that out, and let me know if that needs better explanation.
Good luck!
You can't import a function from another folder directly and for that you have to use this:
import sys
sys.path.insert(0, "../../demo/")
Another step is to rename __main__ to main.
here is the exact example that worked for me:
The tree:
.
├── demo
│ ├── __init__.py
│ ├── main.py
│
└── models
└── grpc
└── allinone_server.py
main.py:
def images_demo():
print("hello there")
The calling file(allinone_server.py):
import sys
sys.path.insert(0, "../../demo/")
import main
main.images_demo()