Can't activate python virtual environment [duplicate] - python

This question already has answers here:
'virtualenv' won't activate on Windows
(23 answers)
Closed last month.
I'm new to python and have been learning how to use a virtual environment.
I followed the tutorial on youtube and created a virtual environment using this command:
python -m venv venv-testing
and tried to activate it using this command:
venv-testing\Scripts\activate.bat
I expected there was my virtual environment name in brackets that showed it activated, but it did nothing. It also didn't show error.
Can someone explain what exactly happened to me? I use Windows and PowerShell.
Thank you very much☺️

From PowerShell, you don't want to start the .bat.
You can, but that will start a new cmd process, in which your environment will activate, and then it will kill that process again.
Instead, you'll want to start the .ps1, i.e.:
venv-testing\Scripts\activate.ps1
This will activate the environment in your current PowerShell process, and the activation will persist after running the script.
Depending on your system's configuration, just running this may work:
venv-testing\Scripts\activate
But you may want to explicitly run the .ps1 instead, to avoid a situation where what you do works on some systems, but not on others.

Related

Unable to debug using gdb with python inside a virtual environment (venv) on windows

I am currently trying to debug a numpy code that I wrote using Gdb. To do that I activate first my virtual environment(on the command prompt) using the following commands (numpy_dev_env is my virtual environment)
numpy_dev_env\Scripts\activate
After that I activate my virtual environment.
gdb
then type "python" to start coding in python (all these steps work good for me).However, I realized that my test were not ran on the virtual environment,but instead on python system available in MINGW. I would appreciate your advice on ways to run the virtual enviroment with gdb.
Thank you,
I tried to configure the .gdbinit file in my home directory.I however did not know how to do with that
edit. clarified that when I run python on gdb the system used is the one available on MINGW and not of my system
Ensure that you are running the correct version of Python: start python in one shell, search for the process id using your task manager, then start gdb and attach the process.
Alternatively, start gdb with the full path to your python inside your environment.

Can't start the Python interpreter in PowerShell or Console anymore using "python", need to type "py" [duplicate]

This question already has answers here:
CMD opens Windows Store when I type 'python'
(12 answers)
Closed 2 years ago.
I have a bit of a oddity going on I can't figure out why it's happening. I've installed the latest version of Python for Windows (3.8.5 64-bit), after removing the previous version, and the version which comes with Visual Studio as part of the Python development package in that IDE. Python installed fine, and also added the Environment variables which I activated in the installer. Now I opened the PowerShell to start the Python interpreter as I've done so often before, typed in python, and Windows 10 opened up the Windows store to suggest to install Python from the store... so, I made sure another time that Python installed the environment variables which it did.
I uninstalled and re-installed Python several times then, also rebooting the computer a few times inbetween, to make sure it could read the changed environment variables. No luck. After browsing the net for some times, I came across a post here which suggested to start the interpreter via the "py" command. Et voilá, it works...
Did they change something in one of the latest versions of Python in regards of the command to start the interpreter? I frankly have no idea what is going on here... starting it via "python" ALWAYS worked, with every former version of Python.
You said you uninstall your previous version of Python, but in your environment variables, are you sure you just have 1 link to python.exe ? Maybe you've got an other path that doesn't work and your command can't execute well.

How do I activate a virtual environment in Windows 7 [duplicate]

This question already has answers here:
Trying to create a virtualenv in python and activate it
(3 answers)
Closed 3 years ago.
I currently need to work on a flask project, to do that, I need to activate a virtual environment for my project. Virtual environment has already been installed at Python37\Scripts folder, but each time I want to activate the virtual environment by running C:\Users\user\Desktop\flask-intro\venv\Scripts>activate. <flask-intro is my project folder>. I get an error that says 'activate' is not recognized as an internal or external command, operable program, or batch file. What could be the issue.
To Create and activate a Virtual Env for project on windows, navigate to your project directory on your command line window and run the following commands
To create - python -m venv your-env-name
To activate - your-env-name\Scripts\activate.bat
You don't need to provide full path to the script, it's enough to use like this: \path\to\env\Scripts\activate and it should be activated.
Also you can check this page for more details about it: https://virtualenv.pypa.io/en/latest/userguide/#activate-script

Make Python 3 default on Mac OS? [duplicate]

This question already has answers here:
How to set Python's default version to 3.x on OS X? [duplicate]
(21 answers)
Closed 4 years ago.
I would like to ask if it is possible to make Python 3 a default interpreter on Mac OS 10 when typing python right away from the terminal? If so, can somebody help how to do it? I'm avoiding switching between the environments.
Cheers
Method 1:
In ~/.bash_profile, set an alias for your python3
alias python='python3'
Method 2(I use this way to keep multiple python versions):
Install python3(the virtualenv python3 on my machine is env-3.5) by virtualenv, in ~/.bash_profile activate certain virtual environment:
source /Users/username/.virtualenvs/env-3.5/bin/activate
I suggest use a virtual environment, it will affect your system even packages messed up.
update:
Did research on anaconda which data_garden commented. Here I post how I installed it:
Go to page https://www.anaconda.com/download/#macos find the package meet your system requirement, for me it's MacOS
Add to system PATH export PATH=$HOME/anaconda3/bin:$PATH into .bash_profile
Search available python versions conda search "^python$"
Create env: conda create -n env-3.6.5 python=3.6.5 , env-3.6.5 is the name of the new created env
Activate env: source activate env-3.6.5, add it into .bash_profile
Done!
You can run conda env list to display all virtual environments you have created.
You can do that by changing alias, typing in something like $ alias python=python3 in the terminal.
If you want the change to persist open ~/.bash_profile using nano and then add alias python=python3. CTRL+O to save and CTRL+X to close.
Then type $ source ~./bash_profile in the terminal.

Schedule a Python script via batch on windows (using Anaconda)

I have a script that i run every day and want to make a schedule for it, i have already tried a batch file with:
start C:\Users\name\Miniconda3\python.exe C:\script.py
And im able to run some basic python commands in it, the problem is that my actual script uses some libraries that were installed with Anaconda, and im unable to use them in the script since Anaconda will not load.
Im working on windows and can't find a way to start Anaconda and run my script there automatically every day.
I would be a bit careful in calling python directly through environment as one never knows if the internals for activate function has changed.
I'm just using basic bat-script to help me out.
SET log_file=%cd%\logfile.txt
call C:\Anaconda3\Scripts\activate.bat
cd \script_directory
python script.py arg1 arg2 > %log_file%
This script saves the log-file wherever the bat is run from, calls the right environment through activate (in this case the standard) and directs all the stdout into log-file for further investigation.
Then just point your Windows Task Scheduler to the script and set the home directory where you want the log-file to appear.
I'd recommend creating an Anaconda environment with the packages you need, then using the python from that environment to run your script. Read about Anaconda environments here
For example...
Say you create an environment called cristians_env
conda create --name cristians_env
and you install the packages you need
conda install pandas
Then, all you need to do is this from your batch script (assuming you use Anaconda 2)
start C:\Users\name\Anaconda2\envs\cristians_env\bin\python C:\script.py
and voila! You're using your anaconda environment from your batch script!
I had a similar problem a few days ago.
What I discovered is that anaconda prompt is nothing but your usual cmd prompt after running an 'activate.bat' script which is located in the anaconda 'Scripts' folder.
So to run your python scripts in anaconda all you need to do is write 2 lines in a batch file. (Open notepad and write the lines mentioned below. Save the file with .bat extension)
call C:\....path to anaconda3\Scripts\activate.bat
call python C:\path to your script\Script.py
Then you schedule this batch file to run as you wish and it will run without problems.
Found a solution, i copied the "activate.bat" file in "C:\Users\yo\Miniconda3\Scripts" and renamed it as schedule.bat and added my script (copy pasted it) on the end of the file.
Then i can schedule a task on windows that executes schedule.bat everyday

Categories