I am new in PyCharm, I use the 2019 CE version.
I have set an environment variable in venv/bin/activate:
# User defined variables:
export DATABASE="postgresql://postgres:postgres#localhost:5432/postgres"
And I can access it using bash:
$ source venv/bin/activate
(venv) $ echo $DATABASE
postgresql://postgres:postgres#localhost:5432/postgres
And when running my script in this environment, it works as expected:
(venv) $ python3 newproject/settings.py
{'database': 'postgresql://postgres:postgres#localhost:5432/postgres'}
It is also automatically done for PyCharm Terminal that natively activates the environment, but I could not succeed in setting it when I run a script.
Anyway, when I specify a Run Configuration to execute a script that needs to access this environment variable, the environment is not loaded automatically before running the script. I have searched almost everywhere, the logical place should be there:
I have found no option (or documentation) to enable virtualenv before running a script. I even tried to add an external tool to manually activate it before it runs the script. But it is useless since executed environments are disjoint: the script does not run in the same environment that venv.
Looking at the python interpreter it seems it is the correct environment.
I know that I can set Environment Variable in this peculiar setup form, but then variables are no longer available when I run the virtual environment, so it is not a solution to me.
My question is then: How can I setup a Run Configuration that activate my environment and run the script within this environment?
Related
I want to write a first_setup.py script, where users can run it for the first time and the script will do the entire setup automatically, namely it will create a virtual environment, activate it, and install the requirements with pip. Users can then start to use the program without worrying about any dependency issue.
Therefore, I used venv together with subprocess library. My Python version is 3.7.5. With the following command, it creates the virtual environment in the working directory:
env_name = ".venv"
subprocess.run(["python", "-m", "venv", env_name])
However, activation doesn't work. I tried to activate it in various ways, for example:
subprocess.run([f"{venv_name}\\Scripts\\Activate.ps1"], shell=True)
This just opens Activate.ps1 in Windows Text Editor like a .txt file (?). I also thought to add .../Scripts/python.exe to the PATH variable, but it didn't work actually.
Furthermore, when the venv created by the script, VS Code pops up a message saying a venv detected, do you want to use it? If I click on Yes, then it changes my interpreter to venv, which is exactly what I want to do, but automatically with the first_setup.py script.
How can I proceed?
Does this first_setup script need to be Python?
The problem is that the activate script sets environment variables for the shell, which is why it’s usually run with ‘source’.
try using bash file:
python3 -m venv venv
source venv/bin/activate
pip install your_library
I have the following setup:
Windows 10
python installed via Anaconda
Virtual environment setup via Anaconda for running and testing my project with pytest
git version control via MINGW
Now I'd like to set a githook that runs all my tests before I push. I have the following problem: I can't activate my virtual environment within the githook.
I tried to activate my anaconda env in the githook script but I can't get it to work. activate as command is not available and calling the whole path ../Anaconda3/Scripts/activate.bat does nothing.
I also tried to use python-githooks to configure the hook for me, but this doesn't seem to work in Windows (it can't read PWD from the environment...)
I'm gratefull for any suggestions.
The solution was to create a .bat-File at the root of the git repository, with:
call C:\...\Anaconda3\Scripts\activate.bat
call activate fs_env
pytest
and to call this file within the pre-push file in .git/hooks with:
./runtests.bat
Platform and software versions:
Mac OS Mojave, VS Code 1.38.0, Python extension installed. Created
virtual environment in project directory using command
python3 -m env.
Modified setting in Python extension,
"python.venvPath": "bin", to handle the bin directory where the python for the virtual
environment is stored.
Situation:
When I launch VS Code using code ., and then open a python file in the folder, the interpreter selected is ./bin/python, however the integrated terminal is not set to the right python executable. If I launch a new terminal it sources the virtual environment (which may be due to the Python extension setting "python.terminal.activateEnvironment": true)
Question:
Is there a way to have the integrated terminal also have the virtual environment sourced?
Or is there a better way to have VS Code activate virtual environment created by python3 -m env .?
Thank you.
Edit:
Just reread the VS Code documentation here - https://code.visualstudio.com/docs/python/environments and this time noticed this below. Wondering if there is a way to kill the existing terminal and then launch one upon VS Code launch...
However, launching VS Code from a shell in which a certain Python environment is activated does not automatically activate that environment in the default integrated terminal. Use the Terminal: Create New Integrated Terminal command after VS Code is running.
Two things. One, "python.venvPath" is meant to point at a directory that contains other virtual environments, not the bin/ directory that has a Python interpreter from a virtual environment. (I also don't know what python3 -m env is supposed to do; did you mean python3 -m venv?)
Two, there isn't a way to make VS Code automatically launch and complete the loading of the Python extension before VS Code creates a terminal if you have the terminal frame open at start-up.
At the time of this writting vscode now has
"python.terminal.activateEnvInCurrentTerminal": true,
so I my global settings.json, F1 > preference: Open Settings (JSON)
"python.venvPath": "D:/miniconda3/envs",
"python.terminal.activateEnvInCurrentTerminal": true,
and in my workspace's settings.json, F1 > preference: Open Workspace Settings (JSON)
"python.defaultInterpreterPath": "D:/miniconda3/envs/my-workspace-venv/python.exe"
And it works for me.
Using Python 3.7.0 on Mac. Trying to use venv module that was added post python 3.4.
I setup my virtual env using python3 venv -m path/to/my/dir - my question is do I need to activate this virtual env to use?
The documentation seem to imply I don't need to?
You don’t specifically need to activate an environment; activation just prepends the virtual environment’s binary directory to your path, so that “python” invokes the virtual environment’s Python interpreter and you can run installed scripts without having to use their full path. However, all scripts installed in a virtual environment should be runnable without activating it, and run with the virtual environment’s Python automatically.
If I don't have to activate, what is the benefit of prepending venv to binary directory? Wouldn't this have to happen regardless for a venv to work?
Activating the virtualenv gives you convenience. It is never required.
Even for scripts that are configured to run with #!/usr/bin/env python, (which looks up the python executable on your path), you could manually update the PATH environment variable:
$ PATH="/path/to/venv/bin" some_script
Activating makes the PATH update stick until you deactivate again, and that can be convenient.
For example, I regularly have several virtualenvs in use at any one time. Some of them are there only to install some command-line tools I have symlinked into my ~/bin/ directory, another is home to a Jupyter notebook, and 2 more are used to quickly verify code snippets in different Python versions with access to 3rd-party libraries. I don't activate any of those.
When you don’t activate a virtualenv, all that happens is that your terminal PATH variable is not updated to put the bin directory of the virtualenv first, so when you enter python or pip or other script without any path into the terminal, the shell will find a different Python binary to run. You can always use any of the commands in the virtualenv bin/ directory by giving the full path to that command.
I'm working on my Flask project in a virtualenv. Every time I start a new terminal, I have to reinitialize these Flask environment variables:
export FLASK_APP="server.py"
export FLASK_DEBUG="1"
My goal is to not have to type them in manually.
I tried writing a Python script that set them, but couldn't make it work. I tried writing a shell script that set them, but Flask would raise an error that said my Python path was incorrect.
Finally, I tried adding the the env vars to the bottom of the virtualenv's activate script. It worked! The env vars are set and Flask runs as expected.
$ source venv/bin/activate
$ flask run
Is it OK to modify the activate script like this? This is just for development purposes.
Yes, setting environment variables in the virtualenv's activate script is fine for managing your development environment. It's described in Flask's docs. They're only active when the env is activated in the terminal, and you have to remember to add them if you create a new env, but there's nothing wrong with it.
With Flask 1.0, you can use dotenv files instead. Install python-dotenv:
pip install python-dotenv
Add a .flaskenv file:
FLASK_APP=server
And the flask command will automatically set them when running a command:
flask run
The advantage of this over messing with the venv is that you can commit this file so it applies anywhere you work on the code.
Modifying venv/bin/activate file is working for you because the environment variable is getting defined inside the virtual environment. When you're using python3 -m venv venv the environment variables are not present in the new virtual environment. Instead of modifying the activate file, you can instead make a shell script which:
Runs venv/bin/activate
Defines the environment variables
Runs the server
First, I tried writing a Python script that set them, but after research, I realized it was not possible(?).
You could use os.environ to do the same from within, but a shell script is better.