Difference between revisions of "Creating a virtual environment in Visual Studio"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(New Virtual Environment)
(Create setup program)
 
(4 intermediate revisions by the same user not shown)
Line 18: Line 18:
 
[[File:Myenv created.png]]
 
[[File:Myenv created.png]]
  
Now you can change from solution explorer to python environments:
+
Now you can change from solution explorer to python environments, and select your virtual environment:
  
 
[[File:PythonEnvironmentswithmyenv.png]]
 
[[File:PythonEnvironmentswithmyenv.png]]
Line 24: Line 24:
 
=Using Virtual Environment=
 
=Using Virtual Environment=
  
In college, you can't use the Visual Studio option to install a package into your virtual environment. This seems to be a permission issue, and you also can't run a command prompt to actually enter the code yourself.
+
In college, you can't use the Visual Studio option (change Overview to Packages) to install a package into your virtual environment. This seems to be a permission issue, and you also can't run a command prompt to actually enter the code yourself. The shortcut links provided within the environment window do work and run.
 +
 
 +
==Create setup program==
 +
In solution explorer, right click the name of your project and click Add, and choose new item:
 +
 
 +
[[File:Python new program.png]]
 +
 
 +
Now choose empty python file, and enter the name 'setup.py':
 +
 
 +
[[File:Name new program.png]]
 +
 
 +
Now enter the following commands:
 +
 
 +
<syntaxhighlight lang=python>
 +
import pip # this will load the pip module
 +
 
 +
pip.main(['install','scpy']) # 'scpy' is the name of a module you want to install
 +
</syntaxhighlight>
 +
 
 +
For a game you could therefore create:
 +
 
 +
<syntaxhighlight lang=python>
 +
import pip
 +
 
 +
pip.main(['install','pygame'])
 +
pip.main(['install','pytmx'])
 +
pip.main(['install','thorpy'])
 +
 
 +
</syntaxhighlight>
 +
 
 +
For a Flask web app you could just do:
 +
 
 +
<syntaxhighlight lang=python>
 +
import pip
 +
 
 +
pip.main(['install','flask'])
 +
</syntaxhighlight>
 +
 
 +
For a Django web app you could just do:
 +
 
 +
<syntaxhighlight lang=python>
 +
import pip
 +
 
 +
pip.main(['install','django'])
 +
</syntaxhighlight>
 +
 
 +
==Running your setup==
 +
Now right click the 'setup.py' in the solution explorer, and choose start:
 +
 
 +
[[File:Start setup.png]]

Latest revision as of 13:40, 30 April 2019

If you have created a new Python based project, you now need to create a virtual environment. You lack the permissions on college machines to install any python modules, on your own machines virtual environments are a good idea for controlling which python modules are installed for which python projects.

New Virtual Environment

In the solution explorer for your new project:

PythonEnvironments.png

You will be able to right click on Python Environments and select 'Add Virtual Environment':

New ve.png

You will need to give the environment a name, this is called 'MyEnv':

Myenv.png

You should also select the version of python, select the highest available. Once completed, your environment should be created here:

Myenv created.png

Now you can change from solution explorer to python environments, and select your virtual environment:

PythonEnvironmentswithmyenv.png

Using Virtual Environment

In college, you can't use the Visual Studio option (change Overview to Packages) to install a package into your virtual environment. This seems to be a permission issue, and you also can't run a command prompt to actually enter the code yourself. The shortcut links provided within the environment window do work and run.

Create setup program

In solution explorer, right click the name of your project and click Add, and choose new item:

Python new program.png

Now choose empty python file, and enter the name 'setup.py':

Name new program.png

Now enter the following commands:

import pip # this will load the pip module

pip.main(['install','scpy']) # 'scpy' is the name of a module you want to install

For a game you could therefore create:

import pip 

pip.main(['install','pygame']) 
pip.main(['install','pytmx'])
pip.main(['install','thorpy'])

For a Flask web app you could just do:

import pip 

pip.main(['install','flask'])

For a Django web app you could just do:

import pip 

pip.main(['install','django'])

Running your setup

Now right click the 'setup.py' in the solution explorer, and choose start:

Start setup.png