I'm using OSQuery throught osqueryi and/or osqueryd on Windows.
I've written some Python extensions (tables) and I try using virtualenv to run these Python extensions.
When I run osqueryi.exe and python extensions separately from command line, the extensions are loaded ok and I can query my python tables. In this scenario, I use virtualenv ok.
When I use extensions.load, with my Python extensions and
osquery.flags has the next content
--disable_extensions=false
--config_path=C:\ProgramData\osquery\osquery.conf
--config_plugin=filesystem
--logger_plugin=filesystem
--logger_path=C:\ProgramData\osquery\log
--extensions_autoload=C:\ProgramData\osquery\extensions.load
In this scenario, osqueryi.exe shows the next error
Traceback (most recent call last):
File "C:\ProgramData\osquery\extensions\my_table.ext", line 3, in <module>
import osquery
ImportError: No module named osquery
First, I activate my virtualenv and later I run osqueryi.exe with this osquery.flags file
In both scenarios, I used the same virtualenv environment with external modules installed via pip.
How can I configure OSquery To use virtualenv with Python Extensions.
Thanks
I have solved this issue.
Looking source code I have seen an environment variable called OSQUERY_PYTHON_PATH
In Windows you should run something like this
set OSQUERY_PYTHON_PATH=<path to python.exe in virtualenv>
Related
I created virtual environment and activate it.
installed packages but unable to import them from virtual environment.
pip freeze:
But getting error trying to import module:
Traceback (most recent call last):
File "z:\Documents\Python\Projects\ProjectName\tempCodeRunnerFile.py", line 1, in <module>
import paramiko
ModuleNotFoundError: No module named 'paramiko'
How to make sure .py file uses virtual environment?
Also if I run
import sys
print(sys.path)
Result:
'C:\\Users\\Username\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages'
So it does not uses virtual environment, is that correct?
In many cases it's not necessary to activate a virtual environment. Typically you could do something like the following from anywhere without activating the virtual environment:
C:\path\to\venv\Scripts\python.exe -m pip somecommand
C:\path\to\venv\Scripts\python.exe different\path\to\script.py
etc.
Additionally you could specify the absolute path to the python.exe as a she-bang at the top of your Python script, and execute the script directly (for example with a double-click) without calling Python explicitly.
#!/path/to/venv/bin/python3
print("Hello world")
References:
https://docs.python.org/3/using/windows.html#shebang-lines
https://docs.python.org/3/library/venv.html#creating-virtual-environments
You should invoke the python that is installed within your environment. That is
source myenv/bin/activate
python path/to/script.py
In Unix, you would add the following #!/usr/bin/env python at the top of your script and this would allow you to run it without specifying the python binary.
source myenv/bin/activate
path/to/script.py
Possibly that works in Windows in the same way or differently.
I want to be able to import FreeCAD into my python scripts, but only in one conda virtual environment. Is there a way to do this without adding FreeCAD to the path at the beginning of each file? I am using Pop!_OS, which should behave like Ubuntu here.
I already found that you can import FreeCAD, but the source I found did so by appending the FreeCAD library location at the beginning of the file: https://www.freecadweb.org/wiki/Embedding_FreeCAD. It looks like you could circumvent this problem by modifying your path variable, and I was able to do so on Windows in my workplace. I just want to do this only for a particular conda virtual environment.
Ideally,
import FreeCAD
will work in a special virtual environment, but not in others.
As mentioned, I got the import statement to work on Windows already by adding FreeCAD's directory to the PATH environment variable. It worked with the default python in command prompt, which should be the anaconda installation, so I think it works in all virtual environments. On Linux, though, I cannot import FreeCAD in python even when I use
PATH=$PATH:/usr/lib/freecad-python3/lib/
which I got from "locate FreeCAD.so" . I get
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'FreeCAD'
It would be really nice to be able to run the same code on both operating systems, and have the PATH modification confined to one virtual environment.
Conda does not look for packages from the PATH environment. Check this answer for the details. But first check whether your package can be installed using pip or conda.
You might go to the virtualenv site packages dir and add the path to the freecad into easy_install.pth
(Solution given at the bottom)
I downloaded the module requests in the command prompt using 'pip install requests'. It downloads fine and works from the command prompt, but when trying to import from the IDLE PyCharm I get the error message:
Traceback (most recent call last):
File "C:/Users/asher/PycharmProjects/begginer_questions/decode_a_web_page.py", line 1, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
By copy-pasting the 'requests' file into the location:
C:/Users/asher/PycharmProjects/begginer_questions
along with the files contained in the file path:
C:/Users/asher/PycharmProjects/begginer_questions/venv/Lib/site-packages/pip-19.0.3-py3.7.egg/pip/_vendor
the code shown below finally gave no error messages.
However, this only seems like a temporary fix. Does anyone know of a permanent fix that could work or any reasons as to why this is happening? The two common problems which often cause this to occur is that multiple versions of Python are downloaded and there is already a file called requests.py, neither are the case for me. I also don't understand why I had to copy-paste the second bunch of files considering they're already contained in begginer_questions.
I'm on Windows 10 with Python 3.7.3
import requests
url = 'https://www.nytimes.com'
r = requests.get(url)
r_html = r.text
Solution:
After a long while I found the answer so for anyone struggling with this problem I hope this helps.
My confusion came from the fact that when a project is created with up to date pip and Python, a virtual environment (venv) is automatically created and is a primary place where the project will look for modules/packages. In the command prompt navigate to your project - in my case it would be:
> cd User\asher\PycharmProjects\begginer_questions
Then enter the following command the activate the virtual environment "venv" pre-made by Python or Pycharm (note this is different for Mac):
venv\Scripts\activate.bat
In this virtual environment enter the command:
pip install requests
The solution seems (and probably is) very obvious, but this should work because sys.path will always look for packages in this virtual environment instead of where ever it was installed when I tried before. Don't forget to deactivate your virtual environment in the command prompt when you're finished installing whatever packages you want by entering:
deactivate
I wrote a Python program in Jupyter Notebook. The program uses libraries installed through Anaconda. I need to get a separate executable Python file that would work on forks of Ubuntu and Debian.
I created the .py file from the .ipynb file through the menu in the Jupyter Notebook:
File -> Download as -> Python (.py).
Next, I try to run .py file through the Terminal in Linux:
>>> python3 name_of_created_file.py
And I get an error:
Traceback (most recent call last):
File "name_of_created_file.py", line 11, in <module>
import pandas as pd
ModuleNotFoundError: No module named 'pandas'
As I understand it, there are not enough software libraries to run the program. On the same computer (Linux) in Jupyter Notebook, my program works well.
How to get a working program separately from Jupiter Notebook? To do this, I need to separately install software libraries?
Can you please check the version of python in your local, whether python or python3. Your issue might be because of two different python versions.
Depending upon your version of python in local machine, you might have to use the same version of pip to install pandas.
Check the version of python you are running in Jupyter.
This is my first time using virtualenv and MySQLdb and I'm getting a strange error. After I setup this virtualenv, I installed MySQLdb from within the virtualenv (with the ENV actually activated). MySQLdb is not installed globally. When I'm in my ENV folder, open a python terminal, and try to import the module I get the following:
import MySQLdb
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named MySQLdb
However, when I do the same thing from the site-packages folder
...ENV/lib/python2.7/site-packages
import MySQLdb
everything seems to work just fine.
Also, when I run
help ('modules')
from either directory, I get very slightly different lists (even though it's telling me I'm using the same python executable). It's like for some reason the same python executable is getting it's modules from different places and the only time it's getting the modules it's supposed to have is deep inside the ENV, in this case the site-packages folder. If I'm not in the site-packages folder, it seems to get a different list and I get no access to MySQLdb.
I'm using Mac OSX Mavericks, XCode is up to date to the best of my knowledge (I don't find I use it very much).
I used this to setup the virtualenv
virtualenv -p /usr/local/Cellar/python/2.7.8/bin/python ENV
which python gives me this both in ENV and in site-packages
...ENV/bin/python
Versions
virtualenv version 1.11.6
python 2.7.8 (not the default python)
UPDATE:
Installed sqlalchemy
(ENV)tims-mbp:ENV timbauer$ pip install sqlalchemy
And am getting the exact same affect. It seems like even though it's using the correct python when inside ENV, that version of python only is getting the correct module list when it's directly inside the site-packages folder. Otherwise it looks like it's pulling from the global list
Found the issue. Inside ENV/bin/pip there was a line that said this
#!/Users/timbauer/Desktop/ENV/bin/python2.7
That needed to say this
#!/Users/timbauer/Desktop/ENV/bin/python
Switching the above out and restarting terminal seems to have fixed everything. I had to delete the virtualenv and reinstall everything to get it to work, but I'd just barely started so this wasn't really that big of a problem.