Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
The project I am working on essentially benchmarks certain aspects of various external python projects. This is easy to do for "official" or "well-supported" projects which allow installing as a module. However, I now need to extend this to cases where the projects are messy or have minimal support and were never expected to be used as modules.
My code needs to import certain classes from these projects. These projects often assume that they are the main piece of code, e.g. if they have a subfolder called "utils" they assume they can just import utils and go about their business. There's a lot of these imports so it's not really feasible to change them all.
Making as few changes as possible to the 3rd party projects, whats the best way to import code from them?
Make sure that all folders are proper Python packages so imports don't fail e.g. create each of the required __init__.py files, then write a setup.py file to bundle them as a library (more information here).
Install the library wherever you want (virtualenv recommended) using pip install path_to_project_folder.
Then import them using the name you've chosen in your setup script, and you should be able to use them almost as they are, since the local imports will work in the package namespace.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Some programming languages provide capability t create a self contained packages that can run on any machine.
For example, dotnet core can self-contained apps per below:
https://learn.microsoft.com/en-us/dotnet/core/deploying/#publish-self-contained
The C# self-contained apps can be single large file, or a directory of all files required to run the application. The package can target Linux, mac or Windows.
In Python, what is the closest feature to self-contained app packages described above?
PyInstaller seems to be the current go to, and it works well in my experience. However, some people have reported that it has very large file sizes, but I've personally never found that to be a major issue.
If you use that, you would also probably need some kind of UI, but that's a separate issue in itself.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Our product imports the library winreg.
I am writing a test which I would like to mock the entire library.
I do not want to mock specific functions, nor enumerate through all of the functions and mock them. I would like to mock the entire library, such that any instance of the library being imported or used instead uses the mock.
Is this possible?
Have you tried patch?
It is not necessarily intuitive to setup though, Python doc example:
#patch('mymodule.SomeClass')
class MyTest(TestCase):
This is a tricky problem. I think you're best bet is to have your real library "winreg" in one folder location and your mock library in another location and do some sneaky PYTHONPATH manipulation when you run the unit tests.
Folder structure and assuming winreg is a python module but could still work if a folder.
real/winreg.py
mock/winreg.py
When you execute your unit tests if on a Linux system:
PYTHONPATH=<directory to mock>/mock:$PYTHONPATH <command to run unittests>
When you run the normal code flip the PYTHONPATH back to the "real" library. You'll just have to make sure that the real library isn't picked up in your PYTHONPATH when running the unit tests which could be tricky depending on your set up.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I've searched the Google and on stackoverflow for this, so if it exists anywhere, my sincerest apologies. I'm (obviously) a newbie to Python, and one of my main concern with finding a new module that does whatever new programming project comes my way is this:
What's preventing a module (once imported) from doing nefarious things, such as logging all keystrokes while the script is executing, then emailing that out?
Am I being paranoid?
Is this not possible in Python?
Is there a website where modules have been code reviewed, and people can download / install them without needing to worry?
Do I have to read the code of every module / sub-module every time I download it to ensure this exact thing isn't happening?
I'm currently using Python 3.5.1 64-bit on Windows 8, but I doubt that's too relevant.
Nothing prevents it. That's one of the benefits of open-source software (in the strictest sense of "source code that I can view"): you can, in theory, examine it to see exactly what it does before actually running it.
In practice, you usually just extend some level of trust to the source:
Is the module in wide use, such that others would have discovered or mentioned a problem in the first place?
Did I get the module from a reputable source?
Does the checksum of my copy match the checksum provided by my source?
If the answer to all three is yes, you can assume that the module isn't doing anything shady without explicitly verifying it yourself.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm using Blender and Python and want to give the .py file to other users to use my script... My script uses external modules (like NumPy) which will rise the errors (like can't find module xxxx).
Not all people can install NumPy (or even Python :D) as many Blender users are just artists.
Another note is that NumPy doesn't work with Blender (I install it in the system's Python, then delete Blender Python so it relies on the system Python).
If you want to distribute your code with external dependencies then you should build a Python egg. The .egg format was created to solve the issue you are dealing with. It is a self-contained release of your code with dependencies and meta-data. Here is some information on how create Python eggs.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I wrote some code that I would like to share, and I would like to follow the best practices in creating/maintaining it's structure. I will host the code on BitBucket, and now I'm thinking about how I should organize it. Is this a good structure?
project_name/
lib/
test/
README
So, this will have the source in lib, and the tests in test. Is this how it's done in Python projects? This is the structure I saw was the most used with Ruby projects. Also, when I run the unit tests, is it considered good practice to do it like this:
set PYTHONPATH=`pwd`/lib
python test/a_test.py
The approach I've come to like is the following:
use distutils and create a setup.py file. (This is mostly useful
when you have lots of extension classes). This will allow you to install the
module system-wide or in a virtualenv directory.
If you want to have serious testing, but stay on the casual side of things,
doctest is what you want, because it can double as "bare-bones" documentation
(when you document the tests and include some commentary on what it's doing).
You can either use doctest to use tests in the docstrings of your code or
keep the tests in some separate .txt files.
You can integrate doctest by extending the setup command with an appropriate cmdclass=... entry in the setup.py file. See this example (CouchDB setup)
for one solution that integrates testing in setup.py. (It uses separate files that
have both the tests and the actual documentation, which is also a possibility).