Running Python File from Command Line with Libraries in venv - python

I'm using Python 3.6 in Windows and using PyCharm. I have a .py file that is using packages installed on a venv which is in a different folder to the .py file.
I'm trying to run this .py from command line and when I do it gives me a ModuleNotFoundError: No module named '<module>'. The file works fine from PyCharm just not from the command line because the packages are in the venv.
How can I get the file to run without errors from command line and keep the packages in the venv?
Many thanks.

You need to activate the virtual environment by callling the activation script:
<path to your environment>\Scripts\activate.bat
as indicated here. Then you will automatically use all the packages installed in this environment when calling your script. Your pycharm is probably set up to automatically use your virtual evnironment

I Think the easiest way to do that is using shebang, and it works both linux and windows.
For windows you just have to add #!.\venv\Scripts\python.exe at the very first line at your .py script file.

Or
source <path to your environment>/bin/activate
on linux

Related

Activating a Virtual Environment when running .bat file using Windows Scheduler

Using pycharm, I finished my first python program.
I am attempting to run a bat file with Windows Scheduler.
The python script has lots of dependencies. They are available in the virtual environment I am using.
I tried activating my virtual environment using the activate.bat file in the directory of the venv.
The code of the scheduled bat file looked like this:
C:\Users\PavelWorkXPS\Anaconda3\envs\TestEnv\Lib\venv\scripts\nt\activate.bat "D:\Dropbox\Toolkit\Python\Unusual_Options_Activity-ToS\main.py" pause
When running this, Pycharm would launch because it is my default .py extension handler.
I modified the bat file to use the interpreter python.exe from within the virtual environment folder, hoping it would have access to the dependencies.
C:\Users\PavelWorkXPS\Anaconda3\envs\TestEnv\python.exe "D:\Dropbox\Toolkit\Python\Unusual_Options_Activity-ToS\main.py" pause
Output would tell me it still can't see the dependencies.
I used this earlier stackoverflow post for help, but it only explained the reason
why pycharm was opening by itself.
Conda was the reason for the problem. When I switched to doing it with a plain virtual environment, it stored all the downloaded repos in a folder inside of the folder with the script. Then I setup the bat file and all worked fine.

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>

using python 2.7 for windows executing an sh file

Windows 10.1 installed
python 2.7 installed
I have an application package that has a file named install.sh and the read me states to run the install.sh file. I have tried numerous times to execute the .sh file without success. I get a name error and that install is not defined. What is the proper syntax to execute the .sh file as directed in my read me?
You need to have a Bash shell installed to run .sh files. Easy options include Git Bash and Cygwin.

virtualenv activation on windows

I have installed virtualenv 1.10 on a Windows 7 machine with Python 2.7 on it.
I have created a virtual env called TESTENV. It is located in C:\
If I run C:\TESTENV\Scripts\activate and then type python followed by:
import sys
print sys.prefix
it outputs C:\TESTENV, as exprected.
But if I run D:\virtualenv_test.py (which is a script containing the two lines of code I used above) it outputs C:\Python27.
I tried adding a shebang line to the script but that doesn't work on Windows.
Why is the script not running in the context of the virtual environment?
virtualenv doesn't change the default application that starts a file.
When you installed Python, the installation associated .py and .pyw files with appropriate C:\Python27\python.exe and C:\Python27\pythonw.exe applications. So when you run (in your cmd) only D:\virtualenv_test.py, cmd looks into the registry and finds the application that is associated with .py files and then runs the file with that application. In this case that is the previously installed C:\Python27\python.exe.
virtualenv activate only adds that particular virtual environments python interpreter at the start of the PATH environment variable, so when you type python in cmd you will get the appropriate python.exe executed.
So the solution to your problem is to run your script as:
python D:\virtualenv_test.py
After you activated the environment.

Python Spyder reset

I was using python(x,y), which came with Spyder. Yesterday, Spyder crashed I can't figure it out how to fix it. I uninstalled python(x,y) and reinstalled, still the same problem.
If I try to open Spyder I get this message:
Spyder crashed during last session
If Spyder does not start at all and before submitting a bug report, please try to reset setting to defaults by running Spyder with the command line option '--reset:
python spyder --reset
Of course, I tried to do the above, but it doesn't seem like I have Spyder in my path. When I try to put
python spyder --reset
in my command prompt window, I get this error message:
python: can't open file 'spyder': [Errno 2] No such file or directory
I made some attempts of trying to add spyder to path, but they all failed. How should I go about adding Spyder to path?
I am using Windows Vista on a 32 bit system.
My Spyder Crash report looks like this:
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\spyderlib\spyder.py", line 1547, in main
mainwindow = run_spyder(app, options)
File "C:\Python27\lib\site-packages\spyderlib\spyder.py", line 1472, in run_spyder
main.setup()
File "C:\Python27\lib\site-packages\spyderlib\spyder.py", line 555, in setup
multithreaded=self.multithreaded)
File "C:\Python27\lib\site-packages\spyderlib\plugins\console.py", line 54, in __init__
light_background=light_background)
File "C:\Python27\lib\site-packages\spyderlib\widgets\internalshell.py", line 101, in __init__
debug, profile)
File "C:\Python27\lib\site-packages\spyderlib\widgets\shell.py", line 698, in __init__
ShellBaseWidget.__init__(self, parent, history_filename, debug, profile)
File "C:\Python27\lib\site-packages\spyderlib\widgets\shell.py", line 61, in __init__
self.history = self.load_history()
File "C:\Python27\lib\site-packages\spyderlib\widgets\shell.py", line 500, in load_history
if rawhistory[1] != self.INITHISTORY[1]:
IndexError: list index out of range
I am terrible with this stuff, so I might be missing something completely obvious or doing something really stupid.
I am wondering if anyone else ran into a similar problem, or can just give suggestions on what to do, and how to do this "reset" thing, or maybe other advice.
Thank You in Advance
python doesn't search spyder in the PATH e.g.:
c:\some\dir> python some_file
Here python tries to read c:\some\dir\some_file file i.e., it looks in the current directory for some_file file.
c:\some\dir> python another_dir\some_file
Here python tries to read c:\some\dir\another_dir\some_file file i.e., it looks in the another_dir directory for some_file file.
c:\some\dir> python C:\path\to\some_file
Here python tries to read C:\path\to\some_file file i.e., it looks in the C:\path\to directory.
If documentation says run: python spyder --reset it assumes that you're in the directory where spyder script lives. It works both from the source checkout scripts directory (if you'd like to run it without installation) and from whatever directory the installation puts scripts in.
Installation process should create spyder or spyder.bat files. If spyder.bat in your PATH then you could run it as:
c:\any\dir> spyder
So instead of python spyder --reset you could try:
c:\any\dir> spyder --reset
The traceback shows that the first executed function is spyderlib.spyder.main(),
source code for the spyder script confirms it, so you can run:
python -c "from spyderlib.spyder import main; main()" --reset
don't worry, it is very easy to reset the spyder. for windows 10 users, you all need to type 'spyder' in 'Cortana' then you can see in apps category something like 'reset spyder setting' just click on that and some black screen will open and your problem will be solved. now you can open your spyder.
I have this problem in windows and it resolved by updating the spyder
pip install -U spyder
I am working with conda virtual environments and if you run
python spyder --reset it it will output the same error message. However, unlike stated above, no need to uninstall anaconda or look for any files.
Just activate your virtual environment as you usually do and write (no need for 'python' in there):
conda activate [name of your virtual env]
spyder --reset
This works for me.
Most of the users' spyder settings are in the user folder .Spyder file name.By deleting it.You will be able to restart the IDE and a new .Spyder directory would be created
For starters in winpython, I too faced this problem of spyder/spyder-reset not starting . And finally this is how it started :
opened command line went to :
D:\manas\mak\crap\WinPython-64bit-3.4.4.2\scripts
typed in spyder.bat
And that opened spyder .
Perhaps the environment variable is stuck at .exe when clicking on the spyder icon and preventing it to load.
Best solution as per my experience: uninstall spyder first, delete all the spyder files under conda > lib > site-packages > ..
1. pip install ipython
2. pip install spyder
3. spyder --reset
I had a similar problem. I had both two installations of Python27---the Ananaconda distribution and the other. Python27 folders appeared before the Anaconda ones in my search path, although Spyder was executed through the Anaconda executable.
It turned out that making the Anaconda distribution folders first in the PYTHONPATH solved the problem.
An easier solution--avoid multiple installations of Python27 if possible.
I tried the method about "python -c "from spyderlib.spyder import main; main()" --reset",but it seems that I don't have the spyderlib.spyder.Then,I solve this problem through install another version of spyder(you can find from anaconda if you use it),you can run:
conda install -c anaconda spyder-app=2.3.8
and it works.
Searched for spyder.bat, per prev answer, and after changing to that directoy - ran the python spyder --reset command.
C:\WinPython-32bit-2.7.3.3\python-2.7.3>cd scripts
C:\WinPython-32bit-2.7.3.3\python-2.7.3\Scripts>python spyder --reset

Categories

Resources