I am creating a single script for setup and running whole Django project.
venv_parent_dir = os.path.abspath(os.path.join(os.getcwd(),os.pardir))
venv_dir = os.path.abspath(os.path.join(venv_parent_dir, 'fvenv'))
subprocess.run(args=['virtualenv', '-p', 'python3', venv_dir])
os.popen('/bin/bash --rcfile %s'%(venv_dir+'/bin/activate'))
With the above code I created a virtual environment then activate this. Now I want to install the requirements.txt file in the activated virtual environment
subprocess.run(args=['pip3', 'install', '-r', 'requirements.txt'])
I tried with subprocess, but it's not installing in the virtual environment, it is installing in the operating system Python.
A the moment, the os.popen command does not affect the environment that subprocess.run runs in. That means that your subprocess.run call is using the system pip3 instead of the pip from the virtualenv. You can use the pip from the virtualenv by using the full path:
import os
pip = os.path.join(venv_dir, 'bin', 'pip')
subprocess.run(args=[pip, 'install', '-r', 'requirements.txt'])
By using /path/to/venv/bin/pip, you don't have to activate the virtual environment first.
Related
I am writing some auto setup code in a command line tool that we have.
This command line tool is installed with poetry, so when someone is invoking the command line tool, it is from within a virtual environment setup by poetry.
The command I am trying to invoke is poetry install for another directory.
I want this command to create a new virtualenv for the other directory.
What I am currently doing is
import subprocess
other_directory = "/some/path"
subprocess.run([f"cd {other_directory} && poetry install && poetry env info"], cwd=other_directory, shell=True, check=True)
But that always installs the dependencies from the other repo into the virtualenv that I am using to run this script. I have also tried to use the path to the system poetry instead of the one in the virtualenv, but that doesn't work either.
The poetry env info command always outputs the info for the current virtualenv:
Virtualenv
Python: 3.10.9
Implementation: CPython
Path: /home/veith/.cache/pypoetry/virtualenvs/my-cli-tool-Qe2jDmlM-py3.10
Executable: /home/veith/.cache/pypoetry/virtualenvs/my-cli-tool-Qe2jDmlM-py3.10/bin/python
Valid: True
And if I manually cd to the other directory and run poetry env info, it always shows that no virtualenv is set up:
Virtualenv
Python: 3.10.9
Implementation: CPython
Path: NA
Executable: NA
The output I am looking for here would be:
Virtualenv
Python: 3.10.9
Implementation: CPython
Path: /home/veith/.cache/pypoetry/virtualenvs/my-other-directory-HASH-py3.10
Executable: /home/veith/.cache/pypoetry/virtualenvs/my-other-directory-HASH-py3.10/bin/python
Valid: True
Is there any way to achieve this? I don't even understand where poetry knows the current virtualenv from if I set the cwd keyword in the subprocess call.
Poetry does not create a new venv if it detects, that the current environment is a venv. This is done by looking for the VIRTUAL_ENV environment variable.
If you unset the variable for the subprocess, poetry will happily create a new venv for you.
E.g.:
import os
import subprocess
other_directory = "/some/path"
current_env = os.environ.copy()
if "VIRTUAL_ENV" in current_env:
del current_env["VIRTUAL_ENV"]
subprocess.run([f"cd {other_directory} && poetry install && poetry env info"], cwd=other_directory, shell=True, check=True, env=current_env)
I have a script that I wrote in Google Collab, and I'm installing the Geopandas, pandas, and descartes packages using !pip install [package] at the top of the script. However, when I run this script from the command line (I open the Terminal in Anaconda), I get a syntax error.
In normal Python, packages are installed by typing:
pip install <pkg_name>
into either PowerShell, Command Prompt or any other shells you have. Pure Python does not support installation via the code script
However, you could in theory write code that runs the shell commands to install the packages, something like the following:
import subprocess
package_names = ['geopandas', 'pandas', 'descartes']
for pkg in package_names:
try:
process = subprocess.run(
['python', '-m', 'pip', 'install', pkg],
shell=True, check=True, text=True,
stderr=subprocess.PIPE, stdout=subprocess.PIPE
)
print(process)
except subprocess.CalledProcessError as e:
print(e.returncode, e.stderr, e.output)
But as you can see, it isn't the simplest code any is extremely bad practise as it does not take into account any environments or cleanliness of the package installations. It is better to create a virtual environment in your project folder and install the dependencies there
cd %userprofile%\path\to\project\folder
py -m venv .\project-env
.\project-env\scripts\activate.bat
pip install geopandas pandas descartes
pip freeze > .\requirements.txt
The above (in Windows Command Prompt, aka cmd.exe) will create a virtual environment, which means that all packages are isolated for that project, which will ensure there are no dependency conflicts, then activate the venv. It will then install the packages and write a requirements file that you can ship with your code
I hope this helps, good luck :D
This is pretty straightforward to activate a virtualenv from powershell of Windows, by ./venv/Scripts/activate command, or with an absolute path like below:
But when I want to execute the same command from a Python script that executes commands in powershell, virtualenv doesn't activate and I can't run pip install something commands inside virtualenv. It means that I can't add packages or even upgrade pip inside virtualenv (Surely because it's not activated correctly).
Note
I'm confident about the implementation of the code because it works clearly for other commands. The only problem might be with C:/temp/venv/Scripts/activate command sent to powershell. Looking for some command like source in Linux to activate that virtualenv.
Here is my code:
installer.py script: runs different commands inside powershell with subprocess, and returns the result.
# installer.py
class Installer:
def run(command):
# Some code here
proc = subprocess.Popen(
[ 'powershell.exe', command ],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
# Some code here
install.py script: sends commands to the Installer class
# install.py
from installer import Installer
installer = Installer()
installer.run('C:/temp/venv/Scripts/activate')
SOLUTION
Turned out I didn't need to activate virtualenv. I could easily run pip install commands with following command sent to subprocess:
installer.run('C:/temp/venv/Scripts/python.exe -m pip install somepackage')
My colleague is unable to debug in VS Code using a Python Azure Function our organization uses. When cloning the repository for the function, she is prompted to create a virtual environment, but no Python version is found under this prompt:
Select a Python interpreter to create a virtual environment
She has tried both manually entering her Python interpreter full path and creating a virtual environment manually in the integrated terminal, but the function will not start when debugging. However, the function does start using func start in the terminal. We have followed all the steps here and ensured all the required packages are installed: https://learn.microsoft.com/en-us/azure/azure-functions/create-first-function-vs-code-python
When I try to reproduce the issue, I'm also prompted to create a virtual environment, but my interpreter shows up and the environment is successfully created. I am using Python 3.7.9 and my colleague is using 3.8.3. We're both using Anaconda versions of Python.
We have also attempted to manually run:
python -m venv .venv
.venv\Scripts\activate && pip install -r requirements.txt
in the terminal, but we are seeing this error when we attempt to debug:
> Executing task: .venv\Scripts\python -m pip install -r requirements.txt <
activate does not accept more than one argument:
['C:\\Users\\ddx\\anaconda3', '/d', '/c', '.venv\\Scripts\\python', '-m', 'pip', 'install', '-r', 'requirements.txt']
We have found a workaround to get debugging work. The activate does not accept more than one argument error happens in tasks.json on the pipInstall requirement. After removing that requirement, we were able to get the function to debug. It doesn't look the function is actually running in the virtual environment, it is using the conda environment. But we are able to debug at least.
I need to use the autogui module to do something in Python. When I run the script, it says that it can't find the autogui module so I installed it with
pip install autogui.
But when I run the script again it still says me this module doesn't exist.
Method 1:
You're probably having trouble setting up your correct Python Interpreter and working within it,try the following in VSCode.
Ctrl + Shift + p
And enter the following in the field.
python: select interpreter
Select the desired environment and reinstall PyAutoGui
Method 2:
Creating a virtual environment for your project where all your packages will be installed and will be isolated from others, and will have no import errors since it's an environment specifically for the project that you're working on.
I assume you use Windows, so open the command line in your working directory, or open your working directory in VSCode and enter the following in the command-line tool that is provided within VSCode.
The Python installers for Windows include pip. You should be able to access pip using:
py -m pip --version
You can make sure that pip is up-to-date by running the following
py -m pip install --upgrade pip
Installing virtual environment
py -m pip install --user virtualenv
Creating a virtual environment
py -m venv env
The second argument is the location to create the virtual environment. Generally, you can just create this in your project and call it env.
venv will create a virtual Python installation in the env folder.
Finally, to activate the environment run the following command
.\env\Scripts\activate
That will activate your environment.
pip install pyautogui
Make sure to change your interpreter to the one that you just created in the env/bin folder and run your code, or you could just enter the path to the *python file located in the env/bin folder.
Try pyautogui - I had the same problem. Instead of autogui, write `pyautogui. Or, if you are running python3 or higher then try:
pip3 install pyautogui.