Get path of installed package - python-3.x

I have a python script, which checks whether a package is installed or not locally, if it is installed then it upgrades it, or else installs it locally.
installed_packages = [dist.project_name for dist in pkg_resources.working_set]
package_name = 'sigma-cli'
if package_name in installed_packages:
print(package_name + ' is already installed, upgrading the package now\n')
pip.main(['install', '--upgrade', package_name])
else:
print(package_name + ' is not installed, installing it now')
pip.main(['install', package_name])
After installing/upgrading, I am trying to use the package as cli:
detection_query = os.system('C:\\Users\\username\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python310\\Scripts\\sigma.exe convert -p crowdstrike_fdr -t splunk sigma_rule.yml')
I would like to make sure that this script runs in every OS and different machines, not just in my local machine, for that I would like to remove the explicit mention of path in the above command and instead let the script find out the path or location to the installed package, hence is there any way I can achieve this?
Thanks in advance for your help!

The script wrappers get put into a path which is environment and platform-dependent. You can find the installation path with stdlib sysconfig:
import sysconfig
script_dir = sysconfig.get_path("scripts")

Related

How to make nodejs know about installed python

Whenever I run the nodejs msi file, it executes a powershell script, complains about not able to find the python and downloads a new version of python everytime. I have added python to my path and even made an environment variable pointing to the executable.
python, PYTHON, %PYTHON% all of these open the interpreter. What else do i need to do for the nodejs to detect the installed python?
node version = 18.12.1
npm version = 8.19.2
You can check the python keyword in the cmd command and check whether the response failed.
Here's a link to how to do commands in python:
https://www.sohamkamani.com/nodejs/executing-shell-commands/

OSError: cannot load library 'gobject-2.0-0': Additionally, ctypes.util.find_library() did not manage to locate a library called 'gobject-2.0-0'

While installing saleor, I have encountered with the below issue.
OSError: cannot load library 'gobject-2.0-0': error 0x7e. Additionally, ctypes.util.find_library() did not manage to locate a library called 'gobject-2.0-0'
I have tried all the solutions given in stack overflow as well as git. Nothing seems to be working.
Can someone please help me out.
Tools installed:
python: 3.8 / 3.9
GTK3
I have also updated the GTK3\bin in the top of the environment variables as said in the other solutions.
Download the https://www.msys2.org/ and install it.
a) install gtk package and python bundles from MSYS2 terminal. We can start this with command shell. and pacman -S mingw-w64-x86_64-gtk3
b) pacman -S mingw-w64-x86_64-python-gobject
Update your $XDG_DATA_HOME and XDG_DATA_DIRS to the installed path for example :
'C:/msys64/mingw64/share'
Reboot your system and check, it will work.
Another option that worked for me is :
Install MSYS2 from https://www.msys2.org.
Install GTK3 DLL Dependencies from here : https://github.com/tschoonj/GTK-for-Windows-Runtime-Environment-Installer/releases
And then set environment path variables to your windows variable path file.
WEASYPRINT_DLL_DIRECTORIES=C:\GTK3\bin

How to install splunkclient for Windows

I am trying to connect to my splunk server via Python on my WIndows laptop.
I downloaded splunklib and splunk-sdk. However, when I run
import splunklib.client as client
I get an error of
ModuleNotFoundError: No module named 'splunklib.client'; 'splunklib' is not a package
Any ideas on why this is occurring and suggestions on to how to fix this or the best way to access Splunk via Python?
Did you properly install the splunk-sdk? You would normally use something like pip to install it.
pip install splunk-sdk
Alternatively, you can install it into the PYTHONPATH
Refer to
https://dev.splunk.com/enterprise/docs/python/sdk-python/gettingstartedpython/installsdkpython/
Windows requires a manual setup of the SDK.
Download the Splunk Software Development Kit for Python as a zip file.
Unzip the zip file into the same directory as your program source.
Add the following line to your source code before import splunklib.client as client:
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "splunk-sdk-python-master"))
Another option is to unzip the sdk to another folder and specify the absolute path in the sys.path.insert().

'python3' is not recognized as an internal or external command, operable program or batch file

I am using Python 3.5.2 version on Windows 7 and tried using python3 app.py. I am getting this error message:
'python3' is not recognized as an internal or external command,
operable program or batch file.
Is there any specific cause about why the python3 command is not working?
I also verified that the PATH is added to environment variables.
There is no python3.exe file, that is why it fails.
Try:
py
instead.
py is just a launcher for python.exe. If you have more than one python versions installed on your machine (2.x, 3.x) you can specify what version of python to launch by
py -2 or
py -3
You can also try this:
Go to the path where Python is installed in your system. For me it was something like C:\Users\\Local Settings\Application Data\Programs\Python\Python37
In this folder, you'll find a python executable. Just create a duplicate and rename it to python3. Works every time.
Python3.exe is not defined in windows
Specify the path for required version of python when you need to used it by creating virtual environment for your project
Python 3
virtualenv --python=C:\PATH_TO_PYTHON\python.exe environment
Python2
virtualenv --python=C:\PATH_TO_PYTHON\python.exe environment
then activate the environment using
.\environment\Scripts\activate.ps1
Yes, I think for Windows users you need to change all the python3 calls to python to solve your original error. This change will run the Python version set in your current environment. If you need to keep this call as it is (aka python3) because you are working in cross-platform or for any other reason, then a work around is to create a soft link. To create it, go to the folder that contains the Python executable and create the link. For example, this worked in my case in Windows 10 using mklink:
cd C:\Python3
mklink python3.exe python.exe
Use a (soft) symbolic link in Linux:
cd /usr/bin/python3
ln -s python.exe python3.exe
In my case I have a git hook on commit, specified by admin. So it was not very convenient for me to change the script (with python3 calls).
And the simplest workaround was just to copy python.exe to python3.exe.
Now I could launch both python and python3.
If python2 is not installed on your computer, you can try with just python instead of python3
For Python 27
virtualenv -p C:\Python27\python.exe django_concurrent_env
For Pyton36
virtualenv -p C:\Python36\python.exe django_concurrent_env
Enter the command to start up the server in that directory:
py -3.7 -m http.server
I had a related issue after installing windows 11, where python3 in cmd would open the windows store. I was able to sort it out between this post and this other one. In short, I reinstalled python and made sure to add it to PATH. Then, in settings, Apps > Apps & Features > App Execution aliases. Here, all I had to do was make sure that every single python .exe (including idle and pip) were turned off EXCEPT FOR the python3.exe alias. Now it works like a charm.
FWIW:
The root of this issue is not with you or with python. Apparently, Microsoft wanted to make installing python easier for young kiddos getting interested in coding, so they automatically add an executable to PATH. For those of us that already have this executable, it can cause these issues.
Found out instead press the play button the top right and it should work in visual studios:
Do not disable according to first answer
Saying python3 in the command will not work by default.
After figuring out the problem with the modules (Solution): https://youtu.be/paRXeLurjE4
Summary:
To import python modules in case of problem to import modules:
Hover over python in search:
Click open in folder
Hover over and right click
click properties
copy everything in path before \python.exe
close those windows
For cmd (administrator):
cd --path that was copied--
then python -m pip install --upgrade pip
cd Scripts
pip install "Name of Package" such as pip install --module (package) --
Im on win10 and have 3.7, 3.8 and 3.10 installed.
For me "python" launches version 3.10 and does not accept commands (like -3.7), "py" launches newest version but does accept commands, and "python3" does nothing.
Uninstalled 3.10 and "python" now does nothing, and "py" launches 3.8.
I am unable to add a comment, but the mlink option presented in this answer above https://stackoverflow.com/a/55229666/8441472 by #Stanislav preserves cross-platform shebangs at the top of scripts (#!/usr/bin/env python3) and launches the right python.
(Even if you install python from python.org, Windows will direct you to the app marketplace nowadays if you type python3 on the command line. If you type python on the same cli it will launch the python.org version repl. It leads to scripts that generate no output, but more likely silently failed completely. I don't know ho common this is but have experienced it on a couple of different devices)
If you have this at the top of your script to ensure you launch python3 and don't feel like editing everything you own, it is not a bad approach at all... lol.

Is there an installer for geodict python library?

I wanted to use the code:
import geodict_lib
locations = geodict_lib.find_locations_in_text(text)
But there seems to be no installer for geodict_lib. How do I install this is Anaconda 3.0 Python 3?
I know this is a year on, but perhaps I could help others who stumble on this. You'll need to place the files in the directory for modules that your installation of Python is monitoring.
First, download the .zip file from GitHub here.
Once you've done that, you can run the following at the command line or terminal:
conda list
This will provide the path to all installed packages in your installation of Python. Move the geodict.zip file you downloaded to that location. You might want to run which python as well (see here) since you may have a few different installations to check for.
Now when you run python import geodict in Python it should run without trouble!

Resources