How to run a python script with dependencies in the command line? - python

I understand that you can run a python script from the command line by entering in the file name. What changes do I need to make with a file that has dependencies?
To be more specific, I'm using Pycharm with a specific Anaconda environment for a project. This environment has packages included such as tensorflow and keras. I can run it directly from Pycharm, however when I try to run from the command line, it gives and error saying that
"there is no model named 'tensorflow'"
What can I do to the command line to signal that it should be using a conda environment, let's call it Env1. I've tried activating the environment in the conda command shell and I still get the same problem. How can I address this?
Thanks

Related

Issue creating conda instance

I am making a project with a GUI using pyqt5 and it requires a conda environment using mini conda. I have tried to make a new environment via following command "conda create -n “gui” python=3.9" and have run into the same error below. I have tried using cmd, anaconda cmd, and anaconda cmd powershell as well as trying to work within the interpreter in pycharm all have produced the same error. I have also tried reinstalling the program to no avail. The errors in the list mention errors in lines of code ie:
Error msg
I have tried using cmd, anaconda cmd, and anaconda cmd powershell as well as trying to work within the interpreter in pycharm all have produced the same error. I have also tried reinstalling the program to no avail. The errors in the list mention errors in lines of code ie:
File "C:\Users\C\miniconda3\lib\site-packages\conda\core\solve.py", line 1058, in _prepare
reduced_index = get_reduced_index(self.prefix, self.channels,

Unable to create process using '...\python.exe' | error in virtual environment

I'm unable to use python within the virtual environment. Python works fine outside of the virtual environment. I'm using Python 3.10.2
I keep on getting the error below when trying to run any python commands.
'C:\Users\User\AppData\Local\Programs\Python\Python310\python.exe'
It might be relevant to mention that I was unable to create the virtual environment through the 'python -m venv env' command. Error generated was
Error: Command '['C:\\Users\\User\\Documents\\Python Projects\\PDFtoText\\env\\Scripts\\python.exe', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 101.
I had to add 'without-pip' to the end of the command to create the virtual environment. Weird thing is, I was able to use pip within the virtual environment without having to manually install it.
The path to python is in the environmental variables.
I tried reinstalling python but that did not help.
Lastly, all these errors started occurring after I downloaded Visual Studio Community 2022.
Short answer, I bet you have a space in your Window's account name (say Your Account is where your account is saved so you have C:\Users\Your Account folder, and there is also a text file C:\Users\Your ("Your" being the first part of your user name). MSVS2022 (maybe earlier versions, too) is known to leave this log file which exposes a bug in Python venv's python launcher. Delete this text file, and your problem should be solved.
See my question/answer for more details.

Auto-completion in Click does not work outside virtualenv

I have a Python script that is designed to make some basic text edits and accepts command line parameters. The whole project is enclosed in a virtual environment too.
I am using python Click module to accept command line parameters, which does support auto completion in bash. I have a basic setup.py file to install the main script as a command or in a virtual environment by using pip install --editable ..
Now let's say main script is called edits. I should use _EDITS_COMPLETE=bash_source edits to generate a bash script to be run and enables auto completion. This does work fine in a virtualenv, where the script gets generated. However, outside of it, the normal script output is given.
In another words, in virtualenv the script returns the correct auto complete script, but outside of it, nothing happens.
So where may the issue with this be? I expect it to generate auto complete script outside virtualenv too.
Link to repository with the script I am trying to use: https://github.com/Astra3/DiscordText
Run pip install click (or pip install -r requirements.txt) in our host environment (outside the virtual env) and it will work.

Has conda deleted my crontab file and my selenium module?

Recently I've installed conda (the package manager) on my Ubuntu.
Now whenever I open a terminal it appears like this:
(base) myName$
I had a crontab task that I was expecting to run but when I checked, the crontab file was empty.
Also, I used to be able to run from the terminal a python script that used selenium. I still can run the program if I use the python IDLE, but now it doesn't work when I run it from the terminal. I get the following error:
ModuleNotFoundError: No module named 'selenium'
Is it possible that conda has done something to my files?
I solved it. I just had to run the cron demon because it had stopped.

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