Auto-completion in Click does not work outside virtualenv - python

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.

Related

Python script runs from terminal in virtual environment, but fails to find pandas with VS Code play button

I am new to VS Code. I have created a virtual environment and selected that as the interpreter, in which I have run
pip install -r requirements
from the terminal which allowed for pandas to be installed in that virtual environment (I believe based on the docs
From that directory in the terminal, I ran
python myscript.py
It works. Generates the output as it should.
I then try to run the same script in the window above and I get the error, no module named pandas. I have not changed the interpreter.
UPDATE
When closing the terminal and then attempting to run the code from the explorer/deug window, I received a failure to run scripts notice. I therefore (based on this post changed my Execution Policies in Windows Shell (separate, not in VS terminal), and now it at least goes into the environment, however I still get the same error, pandas unknown.
UPDATE 2
I noticed in the tutorial that they created the directory and then created the environment within that. I moved my code to be under the virtual environment I created and then it works, both from the terminal and from the debug/explorer window. However here I noticed that the intrepetor is not characterised as a virtual environment but instead looks like the standard version- now there is no option for me to select working_env as an interpretor.
Running pip freeze shows the latest version of pandas which is not what I have installed elsewhere for instance. So this works, but I am now not sure if this is really how I should be using virtual environments.
**UPDATE 3
For whatever reason I can now update the interpreter. When I do however I am back to where I was. I can run the program in the terminal with the interpreteter selected, but running from the debug window I get the error that pandas is missing. Trying to install pandas I am told the requirement is satisfied. Running pip freeze confirms that this is installed. I have the feeling I am somehow installing pandas not in the environment I want
What have I missed in my vscode setup?
Per both the comments, I had a couple of issues here.
For whatever reason during update 2, I couldn't see the interpreter when I was in the folder initially but it was there. It is noted in the docs that this can take a while. Regardless, it must show in the bottom left that you are in the interpreter
Using powershell, I needed to change the execution policy to be unrestricted.
Set-ExecutionPolicy Unrestricted -Scope CurrentUser
Then I hadn't installed pandas in the virtual environment AND with the interpreter. Therefore
. ..\Scripts\activate.ps1
activated the working environment
python -m pip install -r requirements.txt
installed my packages
Running my script was achieved by
python .\myscript.py
and that also runs with the play button (though here I had to press Ctrl+Sft+P type Terminal: Select Default Shell, ensure the terminal was powershell & restart)

Where does venv get pip from?

On a linux(Manjaro) system I had a couple of copies of pip. Some were at the system level and some at the user level and some withing virtual environments where they had been updated to levels higher than the system or user.
I did not like this b/c depending on what I was doing I would have to know what pip is was using, so I wanted to remove them and install pip at the user level and update it to the latest. I have removed every copy of pip that I can find. Finding them I used an exhaustive search on the command line using find command.
If I try to use pip in any terminal now I get that it is not a found command. BUT if I do python -m venv ./venv I find that a copy of pip has made its way into the venv folder.
Where is this copy of pip coming from?
I have check inside ~/.local/, /etc/, /root/, ... like I said, i have done an exhaustive search to see if it is just residing on my system.
EDIT:
I decided to look into the venv module because that was the built-in virtual environment that I have been using.
In /usr/lib/python3.9/venv/ there are __main__.py and __init__.py. Without delving into python usage/implentation specifics I am assuming that the __main__.py file is importing the main() function by use of from . import main in the __main__.py file. Then, in __init__.py the main function calls the create() function as part of the EnvBuilder class defined inside. That create() function eventually leads to _setup_pip() where there is this
cmd = [context.env_exe, '-Im', 'ensurepip', '--upgrade',
'--default-pip']
subprocess.check_output(cmd, stderr=subprocess.STDOUT)
So if pip is not found locally they are going out on the network to get it.
So if this is true then that leads me to want to look at passing a flag to venv when creating the virtual environment so that I can see what it is doing. As of now using python -m venv ./venv just leads to it pausing for a moment while it does the operation then it comes back to the command line prompt. With some sort of output I may have been able to see the download of pip and moved forward quicker.

python flask how to access CLI from terminal when running application from Pycharm

I am using python 2.7 with flask and using pycharm professional IDE, I am running the flask application using a virtual environment from inside pycharm.
When I open a terminal inside pycharm and use CLI commands, it works, and when I open a terminal (regular terminal) outside of the IDE, and trying to use the same command it's not working, the app is still running all the time, and the command is excactly the same.
When I try to activate the same venv outside of the IDE i get permission issue, I assume that it has to do with the venv already being active inside the IDE.
What is the issue? I need to run the same virtual environment in order to use the CLI commands?
How can i access the CLI commands from outside of the IDE?
Thanks
When I try to activate the same venv outside of the IDE I get permission issue
Most likely the problem is all about permission access to the virtual environment's files. Check out access permissions and user:group ownership using ls -al (if you're on Mac or Linux), more info here:
https://linux.die.net/man/1/ls
https://linux.die.net/man/1/chmod
https://linux.die.net/man/1/chown
I assume that it has to do with the venv already being active inside the IDE
Definitely not, you can activate it as many times as you want.
I need to run the same virtual environment in order to use the CLI commands?
At least you have to have all the dependencies installed in your other environment (global or virtual) if you have plans to use one.
when I open a terminal (regular terminal) outside of the IDE, and trying to use the same command it's not working
You'd better post a full error output so that we could check the actual error. Also what command are you trying to run?

Virtualenv within single executable

I currently have an executable file that is running Python code inside a zipfile following this: https://blogs.gnome.org/jamesh/2012/05/21/python-zip-files/
The nice thing about this is that I release a single file containing the app. The problems arise in the dependencies. I have attempted to install files using pip in custom locations and when I embed them in the zip I always have import issues or issues that end up depending on host packages.
I then started looking into virtual environments as a way to ensure package dependencies. However, it seems that the typical workflow on the target machine is to source the activation script and run the code within the virtualenv. What I would like to do is have a single file containing a Python script and all its dependencies and for the user to just execute the file. Is this possible given that the Python interpreter is actually packaged with the virtualenv? Is it possible to invoke the Python interpreter from within the zip file? What is the recommended approach for this from a Python point of view?
You can create a bash script that creates the virtual env and runs the python scripts aswell.
!#/bin/bash
virtualenv .venv
.venv/bin/pip install <python packages>
.venv/bin/python script

virtualenv & subprocess: How to get the correct path to a script

When someone installs my Python package, they can use the command mycmd (it's a console script added to the python bin/ directory). In turn, mycmd launches several other Python console scripts using subprocess:
subprocess.Process('celery arg1 arg2')
subprocess.Process('huey arg1 arg2')
...
(celery and huey commands are installed through my package's pip dependencies.)
This generally works fine, except in the situation where someone invokes mycmd directly without activating its virtualenv. For example, I am trying to use mycmd inside the process control system "circusd". See here where the circus.ini file invokes venv/bin/chaussette directly, without actually activating the venv. If I do this, I get the message celery: No such file or directory, I presume because the virtualenv is not activated and therefore those commands are not found on the path.
How can I ensure that when someone runs mycmd, the correct celery gets run, even if the virtualenv was not activated? (And should also work if the person is not using virtualenv at all, and cross-platform, etc.)
By the way, I am not using subprocess.Process directly, but rather using Honcho, which provides a layer around it.
I solved this simply by adding my virtualenv bin path to the PATH used by circus.

Categories

Resources