David Hancock
David Hancock

Reputation: 473

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 ?

Upvotes: 1

Views: 2496

Answers (2)

Wassadamo
Wassadamo

Reputation: 1376

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.

Upvotes: 0

m09p13
m09p13

Reputation: 54

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:

  1. Open the project settings dialog box in PyCharm.
  2. Expand Project: (your project name) on the left side.
  3. Click on Project Interpreter.
  4. Click on the cog icon on the upper right side of the window and select Add Remote.
  5. Click on the Vagrant radio button.
  6. In the Vagrant Instance Folder box, select the directory your Vagrantfile is located in.
  7. In the Vagrant Host URL box, make sure ssh://[email protected]:2222 is specified.
  8. 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/

Upvotes: 1

Related Questions