How to make sure python script uses virtual environment? - python

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.

Related

No module named 'selenium' (Python3)

I'm trying to import selenium from a python3 application. I've already installed it asi you can see in the first image.
I also configured the vs code with python3 (image 2).
Both if i try to run it from vs code console or with python3 it says the same error.
Exception has occurred: ModuleNotFoundError
No module named 'selenium'
File "/Users/admin/Documents/Bot/bot.py", line 1, in
from selenium import webdriver
I've tried several answers from StackOverflow but it didn't seem to work with me.
Note: i'm not using a virtual environment.
Since apparently i have a lot of python3 installed on my system i just created a virtual environment.
go to your program folder and type:
python3 -m venv virtual-env
this will create a copy of python3 inside your program folder (in /virtual-env/bin/)
then you need to activate it:
source /virtual-env/bin/activate
this will activate your virtual environment, so you can use your own copy of python3 located on your folder.
Once activated, your console will look like this:
(virtual-env) user:program admin$
thanks to everyone and sorry for the lateness

Module can be imported in python but cannot be used from command line

This is essentially the opposite of this problem: Import module works in terminal but not in IDLE
If I start a python session and try to import flask, then it works just fine. Despite that, if I just run flask from the terminal, it is not recognized.
I have confirmed that the locations which pip installs to is on my system path. (I'm on Windows 10 and I typed $Env:Path into Powershell to see that, for example, C:\Users\Zack\AppData\Roaming\Python\Python37\site-packages is in the system path.)
I am also sure that I did pip install --user flask not inside a virtual environment. When I run import flask; print(flask.__file__ I see C:\Users\Zack\AppData\Roaming\Python\Python37\site-packages)
Any other ideas for what could be going wrong?
Edit: I'm trying to run flask run. To check the path python uses, I did (in python) import sys; for i in sys.path: print(i)
As Python language based on Python interpreter, before you use Python, it is necessarily to activate the Python env.

Incude FreeCAD in system path for just one conda virtual environment

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

'requests' imported module won't work on IDLE

(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

OSQuery and Python extensions with virtualenv

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>

Categories

Resources