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
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
Having an issue when using flask and flask-wtf. The issue is that for some reason vscode is defaulting to global rather than the newly created virtual environment. In vscode I will click open folder, create the folder and open it in vscode. Then I'll write my code out and then open terminal in vscode and write: python -m venv my_venv, then on the lower left-hand side I will select it as my virtual environment. I then re-open the terminal, write: pip -V and the pip it is directed to is the folder directory with my_venv in the path name.
Then I pip install flask and flask-wtf, check that they are installed with pip list. Everything looks good until I run and vscode says: ModuleNotFoundError: No module named 'flask-wtf'. But when I go to my global environment and where flask is installed too it runs fine. Something is happening to where the python code is recognizing the original flask and not the one in my virtual environment. How do I ensure that the version of flask is the one in the virtual environment and not others installed on my machine.
Thanks
If you are in your terminal in vscode, it should append your command line with your virtual environment name.
So, usually it would show
PS \path
But if your environment is running, it will show
(venv name) PS \path
If it is now showing, be sure to reactivate your virtual environment from your project directory
For the last 3 days, I have been trying to set up virtual Env on Vs Code for python with some luck but I have a few questions that I cant seem to find the answer to.
Does Vs Code have to run in WSL for me to use venv?
When I install venv on my device it doesn't seem to install a Scripts folder inside the vevn folder. Is this out dated information or am I installing it incorrectly. I am installing onto Documents folder inside my D: drive using python3 - m venv venv. The folder does install and does run in WSL mode but I am trying to run it in clear VsCode so I can use other add-ons such as AREPL that doesn't seem to like being ran in WSL.
For extra context I have oh-my-ZSH set up and using the ubuntu command line on my windows device. Any information will be helpful at this point because I am losing my mind.
venv folder in side D: drive
result
If you have the python extension installed you should be able to select your python interpreter at the bottom.
You should then be able to select the appropriate path
Run Set-ExecutionPolicy Unrestricted -scope process before activating virtual environment.
All the best
You don't have to create a virtual environment under WSL, it will work anywhere. But the reason you don't have a Scripts/ directory is because (I bet) you're running VS Code with git bash and that makes Python think you're running under Unix. In that case it creates a bin/ directory. That will also confuse VS Code because the extension thinks you're running under Windows.
I would either create a virtual environment using a Windows terminal like PowerShell or Command Prompt or use WSL2.
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?
I'll try to explain this, hopefully it makes sense.
I had installed a virtual env a while ago in the directory: desktop/project_website/test_project/env to do some websites.
I also installed scrapy on the desktop/project_website/test_project/env/Scripts dir.
The problem is that whenever I use the command prompt to run scrapy I need to be in this specific directory, so all the files that scrapy creates are made in here, not in the directory where my project is (that is desktop/project_website/Scrapy)
So how would I would I be able to run scrapy in the dir that I want? Thanks.
You can create virtualenv in any directory:
virtualenv some/directory
# or windows
virtualenv some\directory
Then you can activate that virtualenv which will change your $PATH environment variables to read python related things from virtual environments directory instead of your system's:
source some/directory/bin/activate
# or for windows
some\directory\Scripts\activate
and to deactivate type: deactivate
See more at official documentation of virtualenv
Once you have virtualenv activated when you call scrapy the scrapy of virtual environment will be called instead of system one and any packages you install via pip(if the python version in your virtualenv has it) will be installed to the virtual environment.
You can always test what will run by using:
$ which scrapy
some/directory/bin/scrapy
# or for windows
$ where scrapy
some\directory\bin\scrapy