When I open the anaconda prompt the window that opens, shows:
(base) C:\Users\sherv>
So I deactivate it and get:
C:\Users\sherv>
which is the same thing when I open cmd. From here there is there any way that I can reactivate conda? Because the command words like "conda -v" or "activate base", etc, don't work.
Also, the conda prompt is a shortcut so when I right-click and select "open file location", it goes to windows\system32\cmd. So I don't understand why I can't activate conda environments from cmd if it's the same thing?
This is the anaconda prompt shortcut:
When I open file location its the cmd:
I have even tried to add it to that path just in case it might work?
PS. I'm very new to all this and trying to connect some dots; sorry if it's a stupid question.
ok, so the path first opens the cmd (which is why the shortcut points at it, but then from there, from within the cmd, it runs C:\Users\sherv\Anaconda3\Scripts\activate.bat. Is there anyway I can have the batch file commands run from cmd without having to write out the path every time? I added the path for the batch file but it didn't work
The reason why the anaconda prompt shortcut leads to cmd is because it needs to open cmd first and then run a batch file which activates the conda environment. What I had done was to add the batch file to the path but I wasn't using the correct commands to run the conda environment. Therefore, all the keywords such as activate etc didn't work.
Simply type activate.bat and the file which is added to the path is opened which takes you to the base conda environment.
You can use this method to run different programs on different APIs and export the result back and analyse it.
Thanks for downvoting my question :)
I figured out how to do it, in my case with miniconda so it might be slightly different.
Find conda.exe in the Scripts folder. For me it was C:\Users\[SomeUser]\miniconda3\Scripts\conda.exe
Open command prompt and navigate to that same directory
Run conda init cmd.exe - To see more info on this command, you can run conda init --help, where it says "Initialize conda for shell interaction. [Experimental]"
Now after reloading command prompt, from anywhere you should be able to do conda activate WhateverEnvName and it will start it in that environment
If you want to make a batch file that opens to the environment, you can use cmd.exe /k conda activate WhateverEnvName
Alternatively, if you don't want to just open the environment, but actually want to launch a python script directly using that environment, you can type the full path to the python.exe in that environment folder, and then the relative path of the python script to run with that environment.
For example, say you open the command prompt in some arbitrary directory that contains "myscript.py" that you want to run with an environment called "whateverEnv".
You'd do: C:\Users\SomeUserName\miniconda3\envs\whateverEnv\python.exe myscript.py
You could also put that same line into a batch file.
Related
I want to start the conda Prompt from cmd, because I want to use the promt as a terminal in Atom.io.
There is no Conda.exe and the path to conda uses cmd to jump into the prompt. But how do I start it inside of cmd?
I guess what you want is to change to Anaconda shell using cmd, you can find the address for your Anaconda and run the following in your cmd:
%windir%\System32\cmd.exe "/K" "Address"\anaconda3
Or, you can find your Anaconda prompt shortcut, right click on that, and open its properties window. In the properties window, find Target. Then, copy the whole thing in Target and paste it into your cmd.
The answer to your question https://conda.io/projects/conda/en/latest/user-guide/getting-started.html#starting-conda. Also check this guide https://docs.anaconda.com/anaconda/user-guide/.
I have a PyCharm project which uses a virtual env and its own site-packages and I want to be able to run it without needing to open PyCharm everytime.
My current naive solution is runing a batch file which launches the python in the venv and the main script python.exe ../../PythonFiles/Main.pyw. The issue with this is that the console will stay open as its running from a batch process.
You can activate your virtual python environment by this command
source activate yourenvname
then change the directory and go to source directory ../../PythonFiles/ and type
start pythonw Main.pyw
This will help to start the python script in the background & If you don't want to run in the background and keep terminal open remove pyw change it to py extension then run it.
I'd like to use Windows Task Scheduler to run a python script within a virtual environment. I'd like the Scheduler to run a .bat file that will
activate the virtualenv
run the script
These steps work together from the command line, and they work individually in a .bat, but I can't seem to get them to work together from the .bat. It seems the virtualenv is not fully activated when I try to execute the python script and confused as to why.
My .bat looks like this:
call workon venv
cd path/to/Python/proj
python -m script.py
I've tried adding timeouts immediately after the call to workon and tried moving the workon to seperate .bat called from my first file, but the other lines still execute before the virtualenv is activated. Any help is greatly appreciated!
You do not need to activate the virtual environment while running in .bat. All you need to do is to run the python.exe file in your virtual environment.
{path to virtual environment directory}/Scripts/python.exe path/to/your/file.py
In Windows Task Scheduler you can specify the path in which the command prompt will open. So all you need to do is when adding the action, use path to your python in the field Program/script, the name of the file to be run in Add arguments field, and the path to your file.py in Start in field.
P.S if you are reading or writing files in your python file, note that your path will be relative to the one you specify in your start in field in the Action window
You can use an ampersand & operator in a oneliner batch file.
call workon venv & cd path/to/Python/proj & python -m script.py
It will run each command after the other.
You can also double up the ampersand to make it a conditional operator. &&:
call workon venv && cd path/to/Python/proj && python -m script.py
Here the command will only run, if the previous command completed successfully, in other words ERRORLEVEL = 0
Just type
call .\venv\Scripts\activate.bat
in the .bat file and any command afterwards will see the venv activated
for the record call in a cmd pauses the execution of the current script, executes the called one and then resumes.
Create .bat file
write virtual environment activate script location and python file location as below use '&' operator to run two commands.
as below:
"E:\Call Allocation Engine\Development\development_env\Scripts\"activate & python run.py
https://i.stack.imgur.com/31Gkh.png
finally place this file in desired folder and run using cmd.
E:\Call Allocation Engine\Development\Optimisation\Scheduling>file_name.bat
this script will activate virtual environment and run your python code in that environment.
Another way to do this is to make a shortcut of the batch file and then change the "Start in" field.
After that remember to use the full paths in your batch file since it will be running from a difference location.
Edit activate.bat and place this line at the bottom:
python yourscript.py
Schedule the activate.bat itself and it will automatically run your script after the virtual environment activated.
I'm new to coding but I simply want to change directory and run jupyter. The problem is cmd instantly closes once it reaches the jupyter notebook command. Tried cmd /k too but it doesn't have an effect. I must being doing this wrong.
F:
cd directoryname
activate environmentname
jupyter notebook
pause
Solution:
The commands were closing the prompt for some reason when executed in a .bat (they don't when typed). The fix was to type call before the commands.
F:
cd directoryname
call activate environmentname
call jupyter notebook
pause
The commands were closing the prompt for some reason when executed in a .bat (they don't when typed). The fix was to type call before the commands.
F:
cd directoryname
call activate environmentname
call jupyter notebook
pause
Create a simple batch file (jnote.bat):
#echo off
call jupyter notebook "%CD%"
pause
In the same folder create a shortcut to the batch file and rename it to jupyter-notebook.
Open the shortcut properties and change the icon to the jupyter.ico. You will find this in the .\Menu sub-folder in your Anaconda distribution. You should now have a shortcut with the) nice jupyter icon.
Copy the shortcut to all the folders that you use for your notebooks.
Double-click the shortcut to open jupyter-notebook that folder.
For Jupyter, use:
%USERPROFILE%\Anaconda3\python.exe %USERPROFILE%\Anaconda3\cwp.py %USERPROFILE%\Anaconda3 %USERPROFILE%\Anaconda3\python.exe %USERPROFILE%\Anaconda3\Scripts\jupyter-notebook-script.py "**file location**"
for Jupyter lab:
%USERPROFILE%\Anaconda3\python.exe %USERPROFILE%\Anaconda3\cwp.py %USERPROFILE%\Anaconda3 %USERPROFILE%\Anaconda3\python.exe %USERPROFILE%\Anaconda3\Scripts\jupyter-lab-script.py %USERPROFILE%
Save it in a .bat file, with necessary changes at 'UserName' and 'File Location' . Keep it as a single line. Just double click the file to open jupyter notebook at the location.
Note: File Location is the location of the notebook to open.
Alternatively, you can use the same commands to create windows shortcut, without the need for the .bat file:
On the desktop, right-click -> new -> shortcut.
As the location of the item, paste the code above.
Then, you can double click the shortcut to launch jupyter. Unlike the bat file, you can also pin this shortcut to taskbar.
For details on how to setup jupyter lab, check out Running JupyterLab as a Desktop Application in Windows 10
Assuming activate and jupyter are executables or otherwise valid commands, everything should be okay. Since you're saying the cd command is probably the culprit, try the below:
Perhaps you are trying to change to a directory on a different drive... if this is the case you will need to use cd /d directoryname instead.
If this doesn't work, try putting a bunch of pause statements between each command to see exactly where it is breaking.
You can used just that simple code:
d: & activate your_env & jupyter notebook
It's really working for me
I found an alternative way to solve this problem without a .bat.
Search your start menu for "Jupyter Notebook". You should find a shortcut called:
"Jupyter Notebook (environmentname)"
This short cut was created when I set up my environment. In my case environmentname is py35.
To change the directory that Jupyter Notebook starts in find the "Anaconda Prompt" shortcut. Then open "Properties>Shortcut" and change the "Starts in:" field to your desired directory.
If you did not add conda to your path, the following script might be helpful for you. According to https://docs.anaconda.com/anaconda/install/multi-user/ you might even want to check C:\ProgramData\Anaconda but I didn't want to add something to the Batch script that I haven't checked myself.
:: Starts JupyterLab
SET CONDA_ENV=my-specific-conda-environment-name
where conda 2> nul
IF ERRORLEVEL 1 (
ECHO "Conda is not available in your PATH. Guessing the location of the installation..."
CALL C:\Users\%USERNAME%\Anaconda3\Scripts\activate %CONDA_ENV% || (
ECHO "Either Conda or the environment was not found."
PAUSE
)
) ELSE (
CALL activate %CONDA_ENV%
)
CALL jupyter lab || (
ECHO "JupyterLab encountered an error, please check the error message"
PAUSE
)
here is the batch file for this problem
cd directoryname
call activate envName
start jupyter notebook
This is what I use to activate my virtual environment, then run my notebook.
#echo off
call env\Scripts\activate
call jupyter-lab
Saved this as a .bat, so all I have to do to open the notebook is run it.
The best solution I found so far, which works without creating a custom batch file:
Navigate to activate.bat in C:\Users\<youruser>\<Anaconda3 or miniconda3>\Scripts\activate.bat
Right click and create a link to the activate.bat from the Desktop
Right click to change property of the link in the tab link
In Target you enter: %UserProfile%\<Anaconda or miniconda3>\Scripts\activate.bat <your-env-name> && #CALL jupyter lab (when you do not know your environment name you can check them in the file environments.txt in C:\Users\<youruser>\.conda
In the field below you enter your path to the project folder (not to the environment), this is where the script will start.
After this you can edit the icon / title / .. of the link and execute.
This is my code. It starts jupyter notebook in the directory it is saved:
#echo on
cd 'your_path_to_anaconda' /condabin
call activate.bat
cd %~dp0
call jupyter notebook
cmd \k
I have installed the Enthought Python distribution on my computer, but I don't have any idea how to use it. I have PyLab and IDLE but I want to run .py files by typing the following command:
python fileName.py
I don't know where to write this command: IDLE, PyLab or Python.exe or Windows command prompt. When I do this in IDLE it says:
SyntaxError: invalid syntax
Please help me to figure this out.
Open a command prompt: Press ⊞ Win and R at the same time, then type in cmd and press ↵ Enter
Navigate to the folder where you have the ".py" file (use cd .. to go one folder back or cd folderName to enter folderName)
Then type in python filename.py
Indeed, the command to run a Python file should be run in the command prompt. Python should be in your path variable for it to work flexible.
When the python folder is added to path you can call python everywhere in the command prompt, otherwise just in your python install folder.
The following is from the python website:
Windows has a built-in dialog for changing environment variables
(following guide applies to XP classical view): Right-click the icon
for your machine (usually located on your Desktop and called “My
Computer”) and choose Properties there. Then, open the Advanced tab
and click the Environment Variables button.
In short, your path is:
My Computer ‣ Properties ‣ Advanced ‣ Environment Variables In this
dialog, you can add or modify User and System variables. To change
System variables, you need non-restricted access to your machine (i.e.
Administrator rights).
Another way of adding variables to your environment is using the set
command in a command prompt:
set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib
If you do it via My Computer, then look for the line named path in Enviroment Variables. Give that the value of your Python installation folder.
Set PYTHON variable to point to the full path of python.exe.
Then type in command prompt console window:
C:\path_to_folder\ python filename.py