Python Startup

Setup a new python

I used the information from this link https://jeffknupp.com/blog/2014/02/04/starting-a-python-project-the-right-way/

Install Python

First find out where the Python is installed. In my computer i installed it here

C:\Users\<user name>\AppData\Local\Programs\Python\Python36-32\Scripts

Add this to the PATH variable so that it will be helpful in setting up the virtual environment.

Open the command prompt and go to the folder where you want to start a python projects. For example i was working on mywebproj. It will be located in D:\Projects

So from the command prompt, i go to D:\Projects and then create a folder called mywebproj.
Now I have to create a virtualenv in that folder, so that all the python libraries that i’m going to install for this project are local to that project

If the path is already set, then from the command line issue the following command
virtualenv mywebproj

it will create all the structure for a project and related libraries will be copied into that folder. Now if you use pip to install new libraries then it will be installed in that folder local to that project.
First we need to activate the virtualenv, by calling the command
mywebproj\Scripts\activate
This is how it is in my windows machine.
Now the command line will look like this
(mywebproj)D:\Projects\mywebproj

If you already installed Pycharm then open it and then open the project D:\Projects\mywebproj.
After that we need to select the interpreter properly(if it doesn’t automatically find out)
File->Settings->Project mywebproj->Project Intepreter. Now select the Gear Icon and Select Add Local.
Then browse to the project folder and find python.exe D:\Projects\mywebproj\Scripts\python.exe
Once you select the exe, then the Pycharm displays all the libraries installed for that virtual environment.

Mac OS Setup

The assumption is python 3.6 is already setup with pip3 and everything.
First create the project folder where it will be setup and in this case is mywebproj.
it will be set in /Users/<user_name>/Documents/Projects/mywebproj

Now issue the following command from the location /Users/<user_name>/Documents/Projects

pyvenv mywebproj

This will create the virtualenv in the folder mywebproj. Now activate the virtual environment by issuing the following command

source mywebproj/bin/activate

now you are in the virtual environment. You can check the version of the python and pip by issuing the following command

python –version

Python 3.6.2

That tells you that the virtual environment is set to use Python 3.6.2. To make sure that the pycharm is using the correct interpreter, once you open the pycharm and selected the correct project, now

Pycharm Community Edition -> Preferences -> Project Interpreter(mywebproj). Here you can select the correct interpreter if it is not already setup

Now install the required libraries by issuing the command pip. to install the library requests

pip install requests

 

 

Posted in python

Comments are closed.