Run PyCharm project outside of PyCharm - python

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.

Related

Visual Code how can I activate the virtual environment automatically?

Maybe a simple question, but I cannot figure it out. I work within Visual Studio Code on a MacBook. Within a directory I create a virtual environment and have activated that with source .venv/bin/activate scripts and everything works fine.
In the terminal I see (.venv) (base) gaston#MacBook-Pro WebScraping %
When I start visual code another time and open this same directory I do not see the (.venv) so I again give manually the command to activate.
My question is there a way to activate the virtual environment automatically when opening the directory?
VS Code Default Python Interpreter Path
You can do this in a number of ways. If your goal is to have the virtual environment selected immediately at the launch of VS Code, you will have to target that virtual environment as the Python Interpreter in VSC.
https://code.visualstudio.com/docs/python/environments
System Preferences->Settings->Python: Default Interpreter Path
You'll want to point it to your virtualenv Python installation, likely something like:
envFolder/bin/python3
VS Code Launch Configuration Python Interpreter
You can also set a specific launch configuration so that VSCode will still use your system Python instance by default; however, specific launch configurations will have the virtual environment specified.
https://code.visualstudio.com/docs/python/debugging
I did something within settings and now it works.
When I open VSC now it opens the directory and the right environment, just the way I wanted it.
Took me some while to figure what finally did the trick:
I opened settings Command + , typed env in the search box. Scrolled through the results until I saw:
Python > Terminal: Activate Env In Current Terminal
and checked the checkbox. That solved my problem.

Activating a Virtual Environment when running .bat file using Windows Scheduler

Using pycharm, I finished my first python program.
I am attempting to run a bat file with Windows Scheduler.
The python script has lots of dependencies. They are available in the virtual environment I am using.
I tried activating my virtual environment using the activate.bat file in the directory of the venv.
The code of the scheduled bat file looked like this:
C:\Users\PavelWorkXPS\Anaconda3\envs\TestEnv\Lib\venv\scripts\nt\activate.bat "D:\Dropbox\Toolkit\Python\Unusual_Options_Activity-ToS\main.py" pause
When running this, Pycharm would launch because it is my default .py extension handler.
I modified the bat file to use the interpreter python.exe from within the virtual environment folder, hoping it would have access to the dependencies.
C:\Users\PavelWorkXPS\Anaconda3\envs\TestEnv\python.exe "D:\Dropbox\Toolkit\Python\Unusual_Options_Activity-ToS\main.py" pause
Output would tell me it still can't see the dependencies.
I used this earlier stackoverflow post for help, but it only explained the reason
why pycharm was opening by itself.
Conda was the reason for the problem. When I switched to doing it with a plain virtual environment, it stored all the downloaded repos in a folder inside of the folder with the script. Then I setup the bat file and all worked fine.

Write a script file (.bat or .cmd) for Windows, so I can autorun an existing Django Project on any Windows 10 PC with Python preinstalled

I already finished a Django project (development mode) and I just want to run it from any Windows PC with Python pre-installed.
My project folder already includes a venv folder that was created when I create a virtual enviroment in PyCharm.
I create a simple .cmd file and include this commands.
venv\Scripts\activate
python manage.py runserver
I put this file inside the project file on the same level with manage.py file and venv folder.
I don't know why but problem is that command prompt open and close without any error. I tried to run as administrator but nothing changes.
When I manually open the command prompt, change the directory to the project file and run the above commands (enable virtual enviroment and runserver) everything works perfectly fine.
In addition when I add this line on the top
cd D:\PycharmProjects\MyProject
which is the directory of my Project, nothing happens.
I don't want any solution that includes installing anything more than Python (Git for Windows or such), I prefer to write a script file to successfully run python manage.py runserver with success with something that is already preistalled by default with Windows (Command Prompt or PowerShell)
Thanks.
You can give the below a try .. it worked for me on Windows 10.
Remember you must be in the .bat file's folder for this to work.
The extra line will fire up your default browser.
#echo off
title Start Batch
color 0a
start cmd.exe /C "venv\Scripts\activate && cd app && python m_dev.py runserver 0.0.0.0:8001"
start http://127.0.0.1:8001

Batch that starts python virtual environment and runs the virtual python in a project dir

I want to run each day a python script in a virtual environment by recording it in Windows Task Scheduler.
Hence, I need to write a batch that:
Opens a cmd.exe
Starts a virtual environment
Changes the directory to a python project directory
Invokes the virtual python
I wrote this .bat:
cmd /k "cd /d %USERPROFILE%\python_venv\venv1\Scripts & activate & cd /d %PROJECT_DIR%\src"
python main.py
Of course cmd /k does not terminate, and python main.py is not executed.
Sorry in advance if the answer already exists over the Internet but I was not able to find it or to modify a close answer to suit my needs.
[EDIT]
This question is a duplicate of Schedule [Virtualenv Dependent] Python Script with Windows Task Scheduler.
This post has also not been answered yet.
[EDIT 2]
Found an answer here: Run a python script in virtual environment from windows task scheduler
This might be completely irrelevant but I've had a great deal of success using PyCharm + virtual environments with python, you set your VM's file structure up as a project and just add the python environment that you'd like to use with it, it's relatively simple:
Requirements:
Virtual Env
Pycharm
Go to ssh to the virtual env and type which python
Add remote project interpreter (File > Default Settings > Project Interpreter (cog) add remote) and then type in/select the location for the remote python location
You'll need to set up your file system so that PyCharm can also open the project.
NOTE:
Do not turn off your virtual environment without saving your run configurations that will cause pycharm to see your run configurations as corrupt
There's a button on the top right that reads share enable this and your run configs will be saved to a .idea file and you'll have a lot less issues

Using linux console in pycharm

I'm new to pycharm, virtualenv, linux and git.
I've recently begun a journey of using djangoto make webapps. Before I used sublime to make scripts however now a more complex project management system such as pycarm was needed. I actually wanted to get a linux VM and go down that road but was advised that windows python IDE such as pycharm would be suitable
I recently learnt the importance of dependencies and how to use virtualenv. However in this tutorial, under the 'How do I use my shiny new virtual environment?' it starts using commands such as:
ls env
and
which python
Neither of which my pycharm console would understand.
I could use a console emulator such cmder to use the commands but then I would remove the convience of using the IDE's integrated one.
Should I upgrade to a linux VM ? Or can I install a package that allows me to use such commands in PyCharm.
As a bonus question, what are the commands in that tutorial ? are they linux commands? when ever i see $ .... is that the linux console ?
You can accomplish this using Vagrant: https://www.vagrantup.com/
You can use Vagrant and VirtualBox to setup a Linux VM (distro of your choice) and then install all of your Python dependencies in the VM. Once you have that setup, you can tell PyCharm to use the Python interpreter in your VM by following these steps:
Open the project settings dialog box in PyCharm.
Expand Project: (your project name) on the left side.
Click on Project Interpreter.
Click on the cog icon on the upper right side of the window and select Add Remote.
Click on the Vagrant radio button.
In the Vagrant Instance Folder box, select the directory your Vagrantfile is located in.
In the Vagrant Host URL box, make sure ssh://vagrant#127.0.0.1:2222 is specified.
Click OK.
Since Vagrant is compatible with Windows this solution should work for you. I have done it successfully using macOS and it works great. Good luck!
You might find this tutorial useful: https://developer.rackspace.com/blog/a-tutorial-on-application-development-using-vagrant-with-the-pycharm-ide/
I got this to work on Windows 10 with Anaconda Prompt. This terminal which comes with Anaconda, creates a "base" environment with a linux-like virtual machine and your Windows file system (C:\\) mounted to /c, and has bash installed with common Unix commands like cd, ls, chmod, echo, cat, ... Running programs from bash with access to environment variables is much nicer than Windows Powershell etc.
Now to get your Terminal in Pycharm to use Anaconda Prompt instead of cmd.exe, I followed this answer. After installing Anaconda and/or Anaconda Prompt, right-click -> Open File Location -> right-click the shortcut -> Properties -> copy file path. Then use your file path instead.
Conda is great for package environment management. Learn more about it here. For Django + Conda specifically, read here. You can also use pip to install from Python package indexes, github repos, and requirements.txt files instead. Unless you know how Anaconda Prompt works, I don't recommend creating your own environments from scratch. What worked for me was:
(base) C:\Users\wassadamo> conda create -n mynewenvironment --copy base
...
(base) C:\Users\wassadamo> conda activate mynewenvironment
(mynewenvironment) C:\Users\wassadamo> ls
folderA folderB file.txt
Works!
Whenever I try running conda deactivate to leave the base environment, my bash commands would stop working. So clone base as above.
Another tip: if you want to run shell scripts from Terminal within PyCharm with Anaconda Prompt this way, then execute them (e.g. "run.sh") on command line with
bash run.sh
I tried putting this on the first line of my run.sh
#!/usr/bin/bash
And running it with
./run.sh
But this had the effect of running it in an external Anaconda Prompt instance (add sleep, or some user input command to force it to wait and see for yourself). Explicitly running my .sh files with bash had the desired effect of running them in the same shell as I started them in PyCharm Terminal configured with Anaconda Prompt.

Categories

Resources